History

moving examples to top dir

Ge Wang committed 9 years ago

Commit 35ca2f61 parent fd011ef486 files · +-44 −-44Browse files

Showing the changes to examples/string (6 of 486 files). Show all

Unified · Split

6 files
  1. examples/string/escape.ck +22 −0
  2. examples/string/readline.ck +26 −0
  3. examples/string/strops.ck +54 −0
  4. examples/string/strops2.ck +52 −0
  5. examples/string/token.ck +14 −0
  6. examples/string/tostr.ck +6 −0
examples/string/escape.ck+22−0
@@ -0,0 +1,22 @@
1// escape sequences
2
3// newline
4<<< "line1\nline2", "\n" >>>;
5
6// tab
7<<< "a\tb", "\n" >>>;
8
9// audible bell (uh...)
10<<< "bell!\a", "\n" >>>;
11
12// "
13<<< "->\"<-", "\n" >>>;
14
15// end of string
16<<< "here\0can't see me", "\n" >>>;
17
18// wait a bit
19200::ms => now;
20
21// octal (man ascii)
22<<< "bell!\007\060---\061\012", "" >>>;
examples/string/readline.ck+26−0
@@ -0,0 +1,26 @@
1// this is really hacked: ConsoleInput class to be replaced soon
2
3// make a ConsoleInput
4ConsoleInput in;
5// tokenizer
6StringTokenizer tok;
7
8// loop
9while( true )
10{
11 // prompt
12 in.prompt( "enter line of text:" ) => now;
13
14 // read
15 while( in.more() )
16 {
17 // get it
18 tok.set( in.getLine() );
19 // print tokens
20 while( tok.more() )
21 {
22 // print it
23 <<< tok.next(), "" >>>;
24 }
25 }
26}
examples/string/strops.ck+54−0
@@ -0,0 +1,54 @@
1// three strings
2"hello" => string foo;
3"hello" => string bar;
4"there" => string geh;
5
6// makeshift assert
7fun void assert( int condition, string text )
8{
9 if( !condition )
10 {
11 <<< "assertion failed: ", text >>>;
12 me.exit();
13 }
14}
15
16// equality
17assert( foo == foo, "1" );
18assert( foo == bar, "2" );
19assert( "abc" == "abc", "3" );
20assert( "hello" == foo, "4" );
21
22assert( foo != geh, "5" );
23assert( "x" != "y", "6" );
24assert( foo != "there", "7" );
25
26// lt
27assert( foo < geh, "8" );
28assert( foo < "hello!", "9" );
29assert( foo <= foo, "10" );
30assert( foo <= "there", "11" );
31
32// gt
33assert( foo > "abc", "12" );
34assert( foo > "b", "13" );
35assert( foo >= foo, "14" );
36assert( foo >= bar, "15" );
37
38// concatention
39assert( "foo" + "bar" == "foobar", "16" );
40"foo" => string s;
41"bar" +=> s;
42assert( s == "foobar", "17" );
43assert( "bar" + 10 == "bar10", "18" );
44assert( "bar" + 10.0 == "bar10.0000", "19" );
45assert( 10 + "bar" == "10bar", "20" );
46assert( 10.0 + "bar" == "10.0000bar", "21" );
47assert( "foo" + "bar" + "cle" == "foobarcle", "22" );
48assert( "FoO".lower() == "foo", "23" );
49assert( "foo".upper() == "FOO", "24" );
50assert( " foo ".trim() == "foo", "25" );
51assert( " foo ".ltrim() == "foo ", "26" );
52assert( " foo ".rtrim() == " foo", "27" );
53
54<<< "success" >>>;
examples/string/strops2.ck+52−0
@@ -0,0 +1,52 @@
1// a string
2"the quick brown fox jumped over the lazy dog" @=> string str;
3
4// print string
5<<< "str:", str >>>;
6
7// read the first char (as int)
8<<< "str.charAt( 0 ):", str.charAt( 0 ) >>>;
9// read 10th char
10<<< "str.charAt( 10 ):", str.charAt( 10 ) >>>;
11// test character by index
12str.setCharAt( 0, 'T' );
13<<< "str.setCharAt( 0, 'T' ): ", str >>>;
14
15// test substring -- from the 10th char
16<<< "str.substring( 10 ):", str.substring( 10 ) >>>;
17// test substring -- from 10th char, for 10 chars
18<<< "str.substring( 10, 10 ):", str.substring(10, 10) >>>;
19
20// insert
21str.insert( 36, "old " );
22<<< "str.insert( 36, \"old \" ):", str >>>;
23
24// test erase
25str.erase( 40, 5 );
26<<< "str.erase( 40, 5 ):", str >>>;
27
28// test replace
29str.replace( 40, "cat" );
30<<< "str.replace( 45, \"cat\" ):", str >>>;
31// test replace
32str.replace( 4, 4, "slow" );
33<<< "str.replace( 4, 5, \"slow\" ):", str >>>;
34
35// test find -- index of letter, return -1 if not found
36<<< "str.find('b'):", str.find('b') >>>;
37// test find
38<<< "str.find('b', 15):", str.find('b', 15) >>>;
39// test find
40<<< "str.find(\"fox\"):", str.find("fox") >>>;
41// test find
42<<< "str.find(\"fox\", 20):", str.find("fox", 20) >>>;
43
44// test rfind
45<<< "str.rfind('o'):", str.rfind('o') >>>;
46// test rfind
47<<< "str.rfind('o', 20):", str.rfind('o', 20) >>>;
48// test rfind
49<<< "str.find(\"fox\"):", str.find("fox") >>>;
50// test rfind
51<<< "str.find(\"fox\", 20):", str.find("fox", 20) >>>;
52
examples/string/token.ck+14−0
@@ -0,0 +1,14 @@
1// the very hacked StringTokenizer
2
3// make one
4StringTokenizer tok;
5
6// set the string
7tok.set( "tokenize me please!" );
8
9// iterate
10while( tok.more() )
11{
12 // print
13 <<< tok.next(), "" >>>;
14}
examples/string/tostr.ck+6−0
@@ -0,0 +1,6 @@
1// instantiate object
2Event e;
3Object o;
4
5// call
6<<< e.toString() + " --- " + o.toString() >>>;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t