Category talk:Wren-pattern: Difference between revisions
Content added Content deleted
(→Source code: Removed type aliases which are no longer needed.) |
(→Source code: Performance improvement for large strings.) |
||
Line 515: | Line 515: | ||
// Private worker method. |
// Private worker method. |
||
// Looks for a pattern match |
// Looks for a pattern match from a list of codepoints, 'codes', starting from index 'start'. |
||
// 'sc' is the number of codepoints in 'codes'. |
|||
// Returns a Match object if a match is found or null otherwise. |
// Returns a Match object if a match is found or null otherwise. |
||
match_( |
match_(codes, start, sc) { |
||
var tokens = _tokens.toList // use a copy as we might change it |
var tokens = _tokens.toList // use a copy as we might change it |
||
var tc = tokens.count // tokens length |
var tc = tokens.count // tokens length |
||
var ti = 0 // tokens index |
var ti = 0 // tokens index |
||
var t = tokens[ti] // current token |
var t = tokens[ti] // current token |
||
⚫ | |||
var sc = s.count // string codepoints count |
|||
var si = start // string codepoints index |
var si = start // string codepoints index |
||
var c = -1 // string current codepoint |
var c = -1 // string current codepoint |
||
Line 844: | Line 843: | ||
find(s) { |
find(s) { |
||
if (!(s is String)) Fiber.abort("Argument must be a string.") |
if (!(s is String)) Fiber.abort("Argument must be a string.") |
||
var |
var codes = s.codePoints.toList |
||
⚫ | |||
if (sc < _minLen) return null |
if (sc < _minLen) return null |
||
if (_type == Pattern.within) { |
if (_type == Pattern.within) { |
||
var maxStart = sc - _minLen |
var maxStart = sc - _minLen |
||
for (start in 0..maxStart) { |
for (start in 0..maxStart) { |
||
var m = match_( |
var m = match_(codes, start, sc) |
||
if (m) return m |
if (m) return m |
||
} |
} |
||
return null |
return null |
||
} |
} |
||
if (_type == Pattern.start) return match_( |
if (_type == Pattern.start) return match_(codes, 0, sc) |
||
if (_type == Pattern.end) { |
if (_type == Pattern.end) { |
||
var maxStart = sc - _minLen |
var maxStart = sc - _minLen |
||
for (start in 0..maxStart) { |
for (start in 0..maxStart) { |
||
var m = match_( |
var m = match_(codes, start, sc) |
||
if (m && ((start + m.length) == sc)) return m |
if (m && ((start + m.length) == sc)) return m |
||
} |
} |
||
Line 864: | Line 864: | ||
} |
} |
||
if (_type == Pattern.whole) { |
if (_type == Pattern.whole) { |
||
var m = match_( |
var m = match_(codes, 0, sc) |
||
if (!m || m.length < sc) return null |
if (!m || m.length < sc) return null |
||
return m |
return m |
||
Line 880: | Line 880: | ||
} |
} |
||
if (!m) return [] |
if (!m) return [] |
||
⚫ | |||
var matches = [m] |
var matches = [m] |
||
if (m.length == 0) return matches |
if (m.length == 0) return matches |
||
⚫ | |||
var sc = codes.count |
|||
var start = m.index + m.length |
var start = m.index + m.length |
||
while (start + _minLen <= sc) { |
while (start + _minLen <= sc) { |
||
m = match_( |
m = match_(codes, start, sc) |
||
if (m) { |
if (m) { |
||
matches.add(m) |
matches.add(m) |