String

Jan. 3, 2024 || Item:1.3.String

string is used to represent the text data, dynamically allocated array of bytes.

string variable can be indexed from 0 to N-1 (the last element of the array), and they can take on the empty string “”. Reading an element of a string yields a byte.

Syntax:

string variable_name [= initial_value];

where variable_name is a valid identifier and the optional initial_value can be a string literal or the value “” for an empty string. For example:

string myName = "John Smith";

If an initial value is not specified in the declaration, the variable is initialized to “”, the empty string.

A string literal can be assigned to a string or an integral type. If their size differs the literal is right justified and either truncated on the left or zero filled on the left, as necessary.

For example:

byte c = "A";                  // assign to c "A"

bit [10:0] a = "\x41";         // assigns to a ‘b000_0100_0001

bit [1:4][7:0] h = "hello" ;   // assigns to "hello"

A string, string literal, or packed array can be assigned to a string variable. The string variable shall grow or shrink to accommodate the packed array. If the size (in bits) of the packed array is not a multiple of 8, then the packed array is zero filled on the left.

For example:

string s1 = "hello";       // set s1 to "hello"

bit [11:0] b = 12’ha41; 

string s2 = b;             // set s2 to 'h0a41

As a second example:

reg [15:0] r;

integer i = 1;

string b = "";

string a = {"Hi", b};

r = a;               // OK

b = r;               // OK (implicit cast, implementations can issue a warning)

b = "Hi";            // OK

b = {5{"Hi"}};       // OK

a = {i{"Hi"}};       // OK(non constant replication)

r = {i{"Hi"}};       // invalid (non constant replication)

a = {i{b}};          // OK

a = {a,b};           // OK

a = {"Hi",b};        // OK

r = {"H",""};        // yields "H\0" "" is converted to 8'b0

b = {"H",""};        // yields "H"  "" is converted to empty string

a[0] = "h";          // OK same as a[0] = "hi" 

SystemVerilog also includes a number of special methods to work with strings. These methods use the built-in method notation. These methods are described in the following subsections.

1. len()

function int len()

str.len() returns the length of the string, i.e., the number of characters in the string (excluding any terminating character).

If str is "", then str.len() returns 0.

2. putc()

task putc(int i, string s)

task putc(int i, byte c)

str.putc(i, c) replaces the ith character in str with the given integral value.

str.putc(i, s) replaces the ith character in str with the first character in s.

s can be any expression that can be assigned to a string.

putc does not change the size of str: If i < 0 or i >= str.len(), then str is unchanged.

Note: str.putc(j, x) is semantically equivalent to str[j] = x.

3. getc()

function int getc(int i)

str.getc(i) returns the ASCII code of the ith character in str.

If i < 0 or i >= str.len(), then str.getc(i) returns 0.

Note: x = str.getc(j) is semantically equivalent to x = str[j].

4. toupper()

function string toupper()

str.toupper() returns a string with characters in str converted to uppercase.

str is unchanged.

5. tolower()

function string tolower()

str.tolower() returns a string with characters in str converted to lowercase.

str is unchanged.

6. compare()

function int compare(string s)

str.compare(s) compares str and s, as in the ANSI C strcmp function (with regard to lexical ordering and return value), and embedded null bytes are included.

See the relational string operators in Table 1-1.

7. icompare()

function int icompare(string s)

str.icompare(s) compares str and s, like the ANSI C strcmp function (with regard to lexical ordering and return value), but the comparison is case insensitive and embedded null bytes are included.

8. substr()

function string substr(int i, int j)

str.substr(i, j) returns a new string that is a substring formed by characters in position i through j of str.

If i < 0, j < i, or j >= str.len(), substr() returns "" (the empty string).

9. atoi(), atohex(), atooct(), atobin()

function integer atoi()

function integer atohex()

function integer atooct()

function integer atobin()

str.atoi() returns the integer corresponding to the ASCII decimal representation in str. For example:

str = "123";

int i = str.atoi();         // assigns 123 to i.

The conversion scans all leading digits and underscore characters ( _ ) and stops as soon as it encounters any other character, or the end of the string. Returns zero if no digits were encountered. It does not parse the full syntax for integer literals (sign, size, tick, base).

atohex interprets the string as hexadecimal.

atooct interprets the string as octal.

atobin interprets the string as binary.

10. atoreal()

function real atoreal()

str.atoreal() returns the real number corresponding to the ASCII decimal representation in str.

The conversion parses Verilog syntax for real constants. The scan stops as soon as it encounters any character that does not conform to this syntax, or the end of the string. Returns zero if no digits were encoun- tered.

11. itoa()

task itoa(integer i)

str.itoa(i) stores the ASCII decimal representation of i into str (inverse of atoi).

12. hextoa()

task hextoa(integer i)

str.hextoa(i) stores the ASCII hexadecimal representation of i into str (inverse of atohex).

13. octtoa()

task octtoa(integer i)

str.octtoa(i) stores the ASCII octal representation of i into str (inverse of atooct).

14. bintoa()

task bintoa(integer i)

str.bintoa(i) stores the ASCII binary representation of i into str (inverse of atobin).

15. realtoa()

task realtoa(real r)

str.realtoa(r) stores the ASCII real representation of i into str (inverse of atoreal).

SystemVerilog provides a set of operators that can be used to manipulate combinations of string variables and string literals. The basic operators defined on the string data type are listed in Table 1-1.

Table 1-1: String Operators

Table 1-1: String Operators

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



Comments:

Leave a comment: