String Split in SV
Last modified 2017-10-27
Sini
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 splitstringmodule;
string strvlsi;
int poffsetin = 0; int poffsetout = 0; int count = 0;`
string str[$];
function string splitusingdelimiterfn(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; strvlsi = "vlsi:professionals:in:vlsi.pro"; #10; $display($time," STRING = %s",strvlsi); for(int j=0; j<4; j = j+1) begin str[j] = splitusingdelimiterfn(poffsetin,strvlsi,":",poffsetout); $display($time," offsetin=%0d offsetout=%0d",poffsetin,poffsetout); if(poffsetin >= poffsetout) str[j] = strvlsi.substr(poffsetin,strvlsi.len()-1); else poffsetin = poffsetout+1;
$display($time," Splitted String %d = %s",j,str[j]); #10; end end endmodule
Simulation result:
20 STRING = vlsi:professionals:in:vlsi.pro 20 offsetin=0 offsetout=4 20 Splitted String 0 = vlsi 30 offsetin=5 offsetout=18 30 Splitted String 1 = professionals 40 offsetin=19 offsetout=21 40 Splitted String 2 = in 50 offsetin=22 offsetout=21 50 Splitted String 3 = vlsi.pro
The function splitusingdelimiterfn, parses through each character in the string till it finds the delimiter.