| 1 |
// strops.ck some basic string operations |
| 2 |
// |
| 3 |
// for string API documentation, see: |
| 4 |
// https://chuck.stanford.edu/doc/reference/base.html#string |
| 5 |
|
| 6 |
// three strings |
| 7 |
"hello" => string foo; |
| 8 |
"hello" => string bar; |
| 9 |
"there" => string geh; |
| 10 |
|
| 11 |
// makeshift assert |
| 12 |
fun void assert( int condition, string text ) |
| 13 |
{ |
| 14 |
if( !condition ) |
| 15 |
{ |
| 16 |
<<< "assertion failed: ", text >>>; |
| 17 |
me.exit(); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
// equality |
| 22 |
assert( foo == foo, "1" ); |
| 23 |
assert( foo == bar, "2" ); |
| 24 |
assert( "abc" == "abc", "3" ); |
| 25 |
assert( "hello" == foo, "4" ); |
| 26 |
|
| 27 |
assert( foo != geh, "5" ); |
| 28 |
assert( "x" != "y", "6" ); |
| 29 |
assert( foo != "there", "7" ); |
| 30 |
|
| 31 |
// lt |
| 32 |
assert( foo < geh, "8" ); |
| 33 |
assert( foo < "hello!", "9" ); |
| 34 |
assert( foo <= foo, "10" ); |
| 35 |
assert( foo <= "there", "11" ); |
| 36 |
|
| 37 |
// gt |
| 38 |
assert( foo > "abc", "12" ); |
| 39 |
assert( foo > "b", "13" ); |
| 40 |
assert( foo >= foo, "14" ); |
| 41 |
assert( foo >= bar, "15" ); |
| 42 |
|
| 43 |
// concatenation |
| 44 |
assert( "foo" + "bar" == "foobar", "16" ); |
| 45 |
"foo" => string s; |
| 46 |
"bar" +=> s; |
| 47 |
assert( s == "foobar", "17" ); |
| 48 |
assert( "bar" + 10 == "bar10", "18" ); |
| 49 |
assert( 10 + "bar" == "10bar", "19" ); |
| 50 |
assert( ("bar" + 10.0).find("bar10.") == 0, "20" ); |
| 51 |
assert( (10.0 + "bar").find("bar") > 2, "21" ); |
| 52 |
assert( "foo" + "bar" + "cle" == "foobarcle", "22" ); |
| 53 |
assert( "FoO".lower() == "foo", "23" ); |
| 54 |
assert( "foo".upper() == "FOO", "24" ); |
| 55 |
assert( " foo ".trim() == "foo", "25" ); |
| 56 |
assert( " foo ".ltrim() == "foo ", "26" ); |
| 57 |
assert( " foo ".rtrim() == " foo", "27" ); |
| 58 |
assert( ("a"+"b"+"c").length() == 3, "28" ); |
| 59 |
|
| 60 |
<<< "success" >>>; |