-
Notifications
You must be signed in to change notification settings - Fork 22
/
string.fy
168 lines (131 loc) · 3.28 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
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 FancyEnumerable, which means you can use all the
common sequence methods on them, like +map:+, +select:+ etc.
"""
include: FancyEnumerable
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.
\"foo\" * 3 # => \”foofoofoo\"
"""
str = ""
num to_i times: {
str << self
}
str
}
def words {
"""
@return @Array@ of all the whitespace seperated words in @self.
\"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 @FancyEnumerator@ of all bytes (@Fixnum@ values) in @self.
"""
enum = bytes()
def enum each: block {
each(&block)
}
FancyEnumerator new: enum
}
}