Search code examples
specmane

Specman: How to find if a list of bytes exists in another list taking order of the list into account


I have a payload which is a lists of type bytes:

var payload : list of byte;
payload= {1;2;3;4;5;6};

var item1 :list of byte;
item = {3;4;5};


var item2 :list of byte;
item = {1;4};

I would like to implement a code that checks if a list is a sub-list of another. Using "if ..in.." doesn't quite work as it does not take into account the order of the items or if they appear successively or not. I want something that does the following:

  • if (item1 in payload) ...... should return TRUE. Items exist in payload in the same order.

  • if (item2 in payload) ...... should return FALSE because although each element in the list exists in the payload, but the item2 elements do not appear successively in the payload list.

Is there an easy way to achieve this? There must be a build -in function in specman for this.

Thanks


Solution

  • The following code should work:

    if (item.size()==0) {return TRUE};
    for i from 0 to payload.size()-item.size() {
        if (item == payload[i..i+item.size()-1]) {
            return TRUE;
        };
    };
    return FALSE;
    

    Note that this code is quite expensive memory-wise (the list[a..b] syntax creates a new list every time) so if you have memory considerations it should be modified.