/
string.fy
254 lines (196 loc) · 5.24 KB
/
string.fy
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
class String {
"""
Strings are sequences of characters and behave as such.
All literal Strings within Fancy code are instances of the String
class.
They also include @Fancy::Enumerable@, which means you can use all the
common sequence methods on them, like @Fancy::Enumerable#map:@, @Fancy::Enumerable#select:@ etc.
"""
include: Fancy Enumerable
instance_method: '== . documentation: """
Compares @self to another @String@ and returns @true, if equal, @false otherwise.
"""
instance_method: 'uppercase . documentation: """
@return Uppercased version of @self.
Example:
\"hello world\" uppercase # => \"HELLO WORLD\"
"""
instance_method: 'lowercase . documentation: """
@return Lowercased version of @self.
Example:
\"HELLO WORLD\" lowercase # => \"hello world\"
"""
def ++ other {
"""
@other Object to concatenate @self with as a @String@.
@return Concatenation of @self with @other.
Concatenate @self with another Object's @String@ representation.
\"foo\” ++ 42 # => \”foo42\”
"""
self + (other to_s)
}
def whitespace? {
"""
@return @true, if @self consists only of a single whitespace character or is empty, @false otherwise.
Indicates, if a @String@ is empty or a single whitespace character.
"""
empty? or: (self == " ")
}
def blank? {
"""
@return @true if @self consists only of whitespace, @false otherwise.
Indicates, if a @String@ consists only of whitespace.
"""
self =~ /^\s*$/ if_true: {
true
} else: {
false
}
}
def * num {
"""
@num Amount of concatenations to do with @self.
@return @String@ that is the num-fold concatenation of @self.
Returns a @String@ that is the num-fold concatenation of itself.
Example:
\"foo\" * 3 # => \"foofoofoo\"
"""
str = ""
num to_i times: {
str << self
}
str
}
def words {
"""
@return @Array@ of all the whitespace seperated words in @self.
Example:
\"hello world\" words # => [\"hello\", \"world\"]
"""
split
}
def raise! {
"""
Raises a new @StandardError@ with self as the message.
"""
StandardError new: self . raise!
}
def rest {
"""
@return @String@ containing all but the first character.
Returns a @String@ containing all but the first character.
\"hello\" rest # => \"ello\"
"""
from: 1 to: -1
}
def lines {
"""
@return @Array@ of all the lines in @self.
Returns the lines of a @String@ as an @Array@.
"""
split: "\n"
}
def << object {
"""
@object An @Object@ who's @String@ representation will be appended to @self.
@return @self, but modified.
Appends @object's @String@ representation to @self.
Example:
str = \"hello\"
str << \" \"
str << 42
str # => \"hello 42\"
"""
append: $ object to_s
}
def skip_leading_indentation {
"""
Remove leading white space for multi-line strings.
This method expects the first character to be an line return.
"""
str = self
m = /^(\r?\n)*(\s+)/ match(str)
str = str strip()
if: m then: {
pattern = "^ {" ++ (m[2] size()) ++ "}"
rex = Regexp.new(pattern)
str = str gsub(rex, "");
}
str
}
def characters {
"""
@return @Array@ of all characters (as @String@) in @self.
"""
scan: /./
}
def character {
"""
@return @Fixnum@ (byte / ASCII) value of first character in @self.
"""
self bytes each: |c| {
return c
}
return nil
}
def bytes {
"""
@return @Fancy::Enumerator@ of all bytes (@Fixnum@ values) in @self.
"""
enum = bytes()
def enum each: block {
each(&block)
}
Fancy Enumerator new: enum
}
def relative_path: path {
"""
@path Relative path to @self.
@return Absolute @File@ path relative to @self.
Example:
__FILE__ relative: \"../foo/bar\"
"""
File expand_path: $ File dirname(self) + "/" + path
}
def multiline? {
"""
@return @true if @self is a multiline string, @false otherwise.
Example:
\"foo\nbar\" multiline? # => true
\"foo bar\" multiline? # => false
\"\" multiline? # => false
\"\n\n\n\" multiline? # => true
"""
grep: /\n/ . size > 0
}
def main? {
"""
@return @true if @self is the filename of the script that got executed initially.
"""
File expand_path: (ARGV[0] to_s) == self
}
def if_main: main_block else: else_block ({}) {
"""
@main_block @Block@ to be run if @String#:main?@ returns true.
@else_block @Block@ to be called otherwise.
Same as:
if: main? then: else_block else: else_block
"""
if: main? then: main_block else: else_block
}
def snake_cased {
"""
Returns a snake cased version of @self.
"""
r1 = Regexp new("([A-Z]+)([A-Z][a-z])")
r2 = Regexp new("([a-z\d])([A-Z])")
gsub(r1,"\1_\2") gsub(r2,"\1_\2") tr("-", "_") lowercase
}
def camel_cased {
"""
Returns camel cased version of @self which is expected
to be a snake cased @String@.
"""
self split: "_" . map: @{ capitalize } . join
}
}