/
reader.cr
284 lines (247 loc) · 6.98 KB
/
reader.cr
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
struct Char
# A `Char::Reader` allows iterating a `String` by Chars.
#
# As soon as you instantiate a `Char::Reader` it will decode the first
# char in the `String`, which can be accessed by invoking `current_char`.
# At this point `pos`, the current position in the string, will equal zero.
# Successive calls to `next_char` return the next chars in the string,
# advancing `pos`.
#
# Note that the null character `'\0'` will be returned in `current_char` when
# the end is reached (as well as when the string is empty). Thus, `has_next?`
# will return `false` only when `pos` is equal to the string's bytesize, in which
# case `current_char` will always be `'\0'`.
struct Reader
include Enumerable(Char)
# Returns the reader's String.
getter string : String
# Returns the current character.
#
# ```
# reader = Char::Reader.new("ab")
# reader.current_char # => 'a'
# reader.next_char
# reader.current_char # => 'b'
# ```
getter current_char : Char
# Returns the size of the `#current_char` (in bytes) as if it were encoded in UTF-8.
#
# ```
# reader = Char::Reader.new("aé")
# reader.current_char_width # => 1
# reader.next_char
# reader.current_char_width # => 2
# ```
getter current_char_width : Int32
# Returns the position of the current character.
#
# ```
# reader = Char::Reader.new("ab")
# reader.pos # => 0
# reader.next_char
# reader.pos # => 1
# ```
getter pos : Int32
# If there was an error decoding the current char because
# of an invalid UTF-8 byte sequence, returns the byte
# that produced the invalid encoding. Returns `0` if the char would've been
# out of bounds. Otherwise returns `nil`.
getter error : UInt8?
# Creates a reader with the specified *string* positioned at
# byte index *pos*.
def initialize(@string : String, pos = 0)
@pos = pos.to_i
@current_char = '\0'
@current_char_width = 0
@end = false
decode_current_char
end
# Creates a reader that will be positioned at the last char
# of the given string.
def initialize(*, at_end @string : String)
@pos = @string.bytesize
@current_char = '\0'
@current_char_width = 0
@end = false
decode_previous_char
end
# Returns `true` if there is a character left to read.
# The terminating byte `'\0'` is considered a valid character
# by this method.
#
# ```
# reader = Char::Reader.new("a")
# reader.has_next? # => true
# reader.peek_next_char # => '\0'
# ```
def has_next?
!@end
end
# Reads the next character in the string,
# `#pos` is incremented. Raises `IndexError` if the reader is
# at the end of the `#string`.
#
# ```
# reader = Char::Reader.new("ab")
# reader.next_char # => 'b'
# ```
def next_char
@pos += @current_char_width
if @pos > @string.bytesize
raise IndexError.new
end
decode_current_char
end
# Returns the next character in the `#string`
# without incrementing `#pos`.
# Raises `IndexError` if the reader is at
# the end of the `#string`.
#
# ```
# reader = Char::Reader.new("ab")
# reader.peek_next_char # => 'b'
# reader.current_char # => 'a'
# ```
def peek_next_char
next_pos = @pos + @current_char_width
if next_pos > @string.bytesize
raise IndexError.new
end
decode_char_at(next_pos) do |code_point|
code_point.unsafe_chr
end
end
# Returns `true` if there are characters before
# the current one.
def has_previous?
@pos > 0
end
# Returns the previous character, `#pos`
# is decremented.
# Raises `IndexError` if the reader is at the beginning of
# the `#string`
def previous_char : Char
unless has_previous?
raise IndexError.new
end
decode_previous_char.as(Char)
end
# Sets `#pos` to *pos*.
#
# ```
# reader = Char::Reader.new("abc")
# reader.next_char
# reader.next_char
# reader.pos = 0
# reader.current_char # => 'a'
# ```
def pos=(pos)
if pos > @string.bytesize
raise IndexError.new
end
@pos = pos
decode_current_char
pos
end
# Yields successive characters from `#string` starting from `#pos`.
#
# ```
# reader = Char::Reader.new("abc")
# reader.next_char
# reader.each do |c|
# puts c.upcase
# end
# ```
#
# ``` text
# B
# C
# ```
def each : Nil
while has_next?
yield current_char
@pos += @current_char_width
decode_current_char
end
end
private def decode_char_at(pos)
first = byte_at?(pos) || 0u32
if first < 0x80
return yield first, 1, nil
end
if first < 0xc2
invalid_byte_sequence 1
end
second = byte_at?(pos + 1)
if second.nil? || (second & 0xc0) != 0x80
invalid_byte_sequence 1
end
if first < 0xe0
return yield (first << 6) &+ (second &- 0x3080), 2, nil
end
third = byte_at?(pos + 2)
if third.nil? || (third & 0xc0) != 0x80
invalid_byte_sequence 2
end
if first < 0xf0
if first == 0xe0 && second < 0xa0
invalid_byte_sequence 3
end
if first == 0xed && second >= 0xa0
invalid_byte_sequence 3
end
return yield (first << 12) &+ (second << 6) &+ (third &- 0xE2080), 3, nil
end
if first == 0xf0 && second < 0x90
invalid_byte_sequence 3
end
if first == 0xf4 && second >= 0x90
invalid_byte_sequence 3
end
fourth = byte_at?(pos + 3)
if fourth.nil?
invalid_byte_sequence 3
elsif (fourth & 0xc0) != 0x80
invalid_byte_sequence 4
end
if first < 0xf5
return yield (first << 18) &+ (second << 12) &+ (third << 6) &+ (fourth &- 0x3C82080), 4, nil
end
invalid_byte_sequence 4
end
private macro invalid_byte_sequence(width)
return yield Char::REPLACEMENT.ord, {{width}}, first.to_u8
end
@[AlwaysInline]
private def decode_current_char
decode_char_at(@pos) do |code_point, width, error|
@current_char_width = width
@end = @pos == @string.bytesize
@error = error
@current_char = code_point.unsafe_chr
end
end
private def decode_previous_char
if @pos == 0
@end = @pos == @string.bytesize
else
while @pos > 0
@pos -= 1
break if (byte_at(@pos) & 0xC0) != 0x80
end
decode_char_at(@pos) do |code_point, width, error|
@current_char_width = width
@end = @pos == @string.bytesize
@error = error
@current_char = code_point.unsafe_chr
end
end
end
private def byte_at(i)
@string.byte_at(i).to_u32
end
private def byte_at?(i)
@string.byte_at?(i).try(&.to_u32)
end
end
end