Libraries C0.6
3.2 string
The string library contains a few basic functions for working with strings
consisting of ASCII characters. For most nontrivial tasks, it is best to con-
vert back and forth between characters arrays and strings, using the func-
tions string
_
to
_
chararray and string
_
from
_
chararray. The two are iden-
tified in C, but distinguished in C0 to allow a type-safe and memory-safe
implementation. Note that character arrays should contain a trailing ’\0’
character not present in the corresponding strings.
/
*
Return length of s, in characters.
*
May be an O(n) operation.
*
/
int string
_
length(string s);
/
*
Return s[idx] and abort if the idx is out of bound.
*
May be an O(n) operation.
*
/
char string
_
charat(string s, int idx)
/
*
@requires 0 <= idx && idx < string
_
length(s); @
*
/ ;
/
*
Return a new string that is the result of concatenating a
*
and b.
*
/
string string
_
join(string a, string b)
/
*
@ensures string
_
length(\result)
== string
_
length(a) + string
_
length(b); @
*
/ ;
/
*
Returns the substring composed of the characters of s beginning
*
at the index given by start and continuing up to but not
*
including the index given by end. If end == start, the empty
*
string is returned. Aborts if start or end are out of bounds,
*
or end < start.
*
/
string string
_
sub(string a, int start, int end)
/
*
@requires 0 <= start && start <= end && end <= string
_
length(a); @
*
/
/
*
@ensures string
_
length(\result) == end - start; @
*
/ ;
/
*
Compare strings lexicographically
*
/
bool string
_
equal(string a, string b);
/
*
string
_
compare(a, b) returns
*
\result < 0 if a comes lexicographically before b
*
\result == 0 if a and b are the same string
*
\result > 0 if a comes lexicographically after b
*
/
int string
_
compare(string a, string b);
/
*
Create strings from other basic values
*
/
string string
_
fromint(int i);
string string
_
frombool(bool b);
SUNDAY 23
RD
JANUARY, 2022