-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.dua
150 lines (111 loc) · 2.27 KB
/
string.dua
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import "../lib/string.dua"
// A quick hack to replace the extern variable
// to avoid both compilation and linking errors
int _0 = { untrack(__IS_RANDOM_SEED_SET); 0 };
bool __IS_RANDOM_SEED_SET = false;
int _1 = { untrack(__INPUT_STREAM_BUFFER_LEN); 0 };
int __INPUT_STREAM_BUFFER_LEN = 4096;
nomangle int printf(str message, ...);
// Case Simple string
// Outputs "Hello world"
int main()
{
String s("Hello world");
printf("%s", s.c_str());
}
// Case String concatenation with byte*
// Outputs "Hello world"
int main()
{
String s("Hello");
var c = s + " world";
printf("%s", c.c_str());
}
// Case byte* concatenation with String
// Outputs "Hello world"
int main()
{
String s("world");
var c = "Hello " + s;
printf("%s", c.c_str());
}
// Case String concatenation with String
// Outputs "Hello world"
int main()
{
String s1("Hello ");
String s2("world");
var c = s1 + s2;
printf("%s", c.c_str());
}
// Case Concatenation using compound assignment
// Outputs "Hello there you're welcome"
int main()
{
String s("Hello");
var c = s + " there";
c += " you're";
c += " welcome";
printf("%s", c.c_str());
}
// Case Substring
// Outputs "Hello world"
int main()
{
String s("Hello Hello world world");
var substr = s.substring(6, 17);
printf("%s", substr.c_str());
}
// Case Pushing
// Outputs "Hello world"
int main()
{
String s("Hello");
s.push(' ');
s.push('w');
s.push('o');
s.push('r');
s.push('l');
s.push('d');
printf("%s", s.c_str());
}
// Case Popping
// Outputs "Hello world"
int main()
{
String s1("Hello");
String s2("dlrow ");
while (!s2.is_empty())
s1.push(s2.pop());
// An extra check
s1.push(s1.pop());
s1.push(s1.pop());
s1.push(s1.pop());
s1.push(s1.pop());
printf("%s%s", s1.c_str(), s2.c_str());
}
// Case Popping an empty string
// Returns -1
int main()
{
String s;
s.pop();
}
// Case Assigning strings
// Outputs "Hello world"
int main()
{
String world = "Hello";
String hello = world;
world = "world";
printf("%s %s", hello.c_str(), world.c_str());
}
// Case Self-assignment
// Outputs "Hello"
int main()
{
String s = "Hel";
s += "lo";
s = s;
printf("%s", s.c_str());
}