String Split in SV

In our verification environment, we may need to do some kind of string manipulations. In scripting languages like perl, this is done by using just a method (split). In SV, we need to do it in a roundabout way by parsing all the characters by using getc method.

Below example shows how to split a string using ” : ” as delimiter.

module split_string_module;

string str_vlsi;

int p_offset_in = 0;
int p_offset_out = 0;
int count = 0;`

string str[$];

function string split_using_delimiter_fn(input int offset, string str,string del,output int cnt);
  for (int i = offset; i < str.len(); i=i+1) 
    if (str.getc(i) == del) begin
       cnt = i;
       return str.substr(offset,i-1);
     end
endfunction

initial begin
   #10; str_vlsi = "vlsi:professionals:in:vlsi.pro";
   #10; $display($time,"   STRING = %s",str_vlsi);
   for(int j=0; j<4; j = j+1) begin
       str[j] = split_using_delimiter_fn(p_offset_in,str_vlsi,":",p_offset_out);
       $display($time,"   offset_in=%0d offset_out=%0d",p_offset_in,p_offset_out);
       if(p_offset_in >= p_offset_out) 
          str[j] = str_vlsi.substr(p_offset_in,str_vlsi.len()-1);
       else 
          p_offset_in = p_offset_out+1;

       $display($time,"   Splitted String %d = %s",j,str[j]);
       #10;
    end
end
endmodule

Simulation result:

                  20   STRING = vlsi:professionals:in:vlsi.pro
                  20   offset_in=0 offset_out=4
                  20   Splitted String           0 = vlsi
                  30   offset_in=5 offset_out=18
                  30   Splitted String           1 = professionals
                  40   offset_in=19 offset_out=21
                  40   Splitted String           2 = in
                  50   offset_in=22 offset_out=21
                  50   Splitted String           3 = vlsi.pro

The function split_using_delimiter_fn, parses through each character in the string till it finds the delimiter.

2 comments on “String Split in SV

Leave a Reply to anonymous Cancel reply

Your email address will not be published. Required fields are marked *