-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_operations.casa
More file actions
66 lines (65 loc) · 2.05 KB
/
string_operations.casa
File metadata and controls
66 lines (65 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# String operations
# Demonstrates string methods, char type, and cstr type
import "std"
# String length
"=== length ===" print "\n" print
"hello".length print "\n" print
"".length print "\n" print
# Char access
"=== at ===" print "\n" print
0 "hello".at print "\n" print
4 "hello".at print "\n" print
# Char literals
"=== char ===" print "\n" print
'A' print "\n" print
'\n' (int) print "\n" print
# String equality (== and != compare by content)
"=== eq ===" print "\n" print
"hello" "hello" == print "\n" print
"hello" "world" == print "\n" print
"" "" == print "\n" print
"hello" "world" != print "\n" print
# String concatenation
"=== concat ===" print "\n" print
"hello" " world" str::concat print "\n" print
"" "test" str::concat print "\n" print
# Substring extraction
"=== substring ===" print "\n" print
"hello world" 0 5 str::substring print "\n" print
"hello world" 6 5 str::substring print "\n" print
# String find
"=== find ===" print "\n" print
"hello world" "world" str::find print "\n" print
"hello world" "xyz" str::find print "\n" print
"hello world" "hello" str::find print "\n" print
# Starts with and ends with
"=== starts_with ===" print "\n" print
"hello world" "hello" str::starts_with print "\n" print
"hello world" "world" str::starts_with print "\n" print
"=== ends_with ===" print "\n" print
"hello world" "world" str::ends_with print "\n" print
"hello world" "hello" str::ends_with print "\n" print
# Building a string with alloc and store
"=== alloc + store ===" print "\n" print
14 alloc (str) = greeting
5 greeting (ptr) store64
'H' 0 greeting.set
'e' 1 greeting.set
'l' 2 greeting.set
'l' 3 greeting.set
'o' 4 greeting.set
'\0' 5 greeting.set
greeting print "\n" print
# cstr conversion
"=== as_cstr ===" print "\n" print
"hello".as_cstr print "\n" print
# Character classification
"=== char classification ===" print "\n" print
'5'.is_digit print "\n" print
'a'.is_digit print "\n" print
'A'.is_upper print "\n" print
'a'.is_lower print "\n" print
'A'.is_alpha print "\n" print
'0'.is_alpha print "\n" print
' '.is_space print "\n" print
'A'.is_space print "\n" print