Class: Sass::Util::MultibyteStringScanner
- Inherits:
-
StringScanner
- Object
- StringScanner
- Sass::Util::MultibyteStringScanner
- Defined in:
- lib/sass/util/multibyte_string_scanner.rb,
lib/sass/util/multibyte_string_scanner.rb,
lib/sass/util/multibyte_string_scanner.rb
Overview
A wrapper of the native StringScanner class that works correctly with multibyte character encodings. The native class deals only in bytes, not characters, for methods like [#pos] and [#matched_size]. This class deals only in characters, instead.
Class Method Summary collapse
Instance Method Summary collapse
- #byte_matched_size
- #byte_pos
- #check(pattern)
- #check_until(pattern)
- #get_byte
- #getbyte
- #getch
-
#initialize(str) ⇒ MultibyteStringScanner
constructor
A new instance of MultibyteStringScanner.
- #is_a?(klass) ⇒ Boolean
- #match?(pattern) ⇒ Boolean
- #matched_size
- #peek(len) (also: #peep)
- #pos (also: #pointer)
- #pos=(n)
- #reset
- #rest_size
- #scan(pattern)
- #scan_full(pattern, advance_pointer_p, return_string_p)
- #scan_until(pattern)
- #search_full(pattern, advance_pointer_p, return_string_p)
- #skip(pattern)
- #skip_until(pattern)
- #string=(str)
- #terminate (also: #clear)
- #unscan
Constructor Details
#initialize(str) ⇒ MultibyteStringScanner
Returns a new instance of MultibyteStringScanner.
12 13 14 15 16 17 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 12
def initialize(str)
super(StringScanner.new(str))
@mb_pos = 0
@mb_matched_size = nil
@mb_last_pos = nil
end
|
Class Method Details
.new(str)
39 40 41 42 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 39
def self.new(str)
return StringScanner.new(str) if str.ascii_only?
super
end
|
Instance Method Details
#byte_matched_size
45 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 45
alias_method :byte_matched_size, :matched_size
|
#byte_pos
44 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 44
alias_method :byte_pos, :pos
|
#check(pattern)
47 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 47
def check(pattern); _match super; end
|
#check_until(pattern)
48 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 48
def check_until(pattern); _matched super; end
|
#get_byte
62 63 64 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 62
def get_byte
raise "MultibyteStringScanner doesn't support #get_byte."
end
|
#getbyte
66 67 68 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 66
def getbyte
raise "MultibyteStringScanner doesn't support #getbyte."
end
|
#getch
49 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 49
def getch; _forward _match super; end
|
#is_a?(klass) ⇒ Boolean
19 20 21 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 19
def is_a?(klass)
__getobj__.is_a?(klass) || super
end
|
#match?(pattern) ⇒ Boolean
50 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 50
def match?(pattern); _size check(pattern); end
|
#matched_size
51 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 51
def matched_size; @mb_matched_size; end
|
#peek(len) Also known as: peep
52 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 52
def peek(len); string[@mb_pos, len]; end
|
#pos Also known as: pointer
54 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 54
def pos; @mb_pos; end
|
#pos=(n)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 70
def pos=(n)
@mb_last_pos = nil
# We set position kind of a lot during parsing, so we want it to be as
# efficient as possible. This is complicated by the fact that UTF-8 is a
# variable-length encoding, so it's difficult to find the byte length that
# corresponds to a given character length.
#
# Our heuristic here is to try to count the fewest possible characters. So
# if the new position is close to the current one, just count the
# characters between the two; if the new position is closer to the
# beginning of the string, just count the characters from there.
if @mb_pos - n < @mb_pos / 2
# New position is close to old position
byte_delta = @mb_pos > n ? -string[n...@mb_pos].bytesize : string[@mb_pos...n].bytesize
super(byte_pos + byte_delta)
else
# New position is close to BOS
super(string[0...n].bytesize)
end
@mb_pos = n
end
|
#reset
93 94 95 96 97 98 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 93
def reset
@mb_pos = 0
@mb_matched_size = nil
@mb_last_pos = nil
super
end
|
#rest_size
56 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 56
def rest_size; rest.size; end
|
#scan(pattern)
57 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 57
def scan(pattern); _forward _match super; end
|
#scan_full(pattern, advance_pointer_p, return_string_p)
100 101 102 103 104 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 100
def scan_full(pattern, advance_pointer_p, return_string_p)
res = _match super(pattern, advance_pointer_p, true)
_forward res if advance_pointer_p
return res if return_string_p
end
|
#scan_until(pattern)
58 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 58
def scan_until(pattern); _forward _matched super; end
|
#search_full(pattern, advance_pointer_p, return_string_p)
106 107 108 109 110 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 106
def search_full(pattern, advance_pointer_p, return_string_p)
res = super(pattern, advance_pointer_p, true)
_forward res if advance_pointer_p
_matched((res if return_string_p))
end
|
#skip(pattern)
59 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 59
def skip(pattern); _size scan(pattern); end
|
#skip_until(pattern)
60 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 60
def skip_until(pattern); _matched _size scan_until(pattern); end
|
#string=(str)
112 113 114 115 116 117 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 112
def string=(str)
@mb_pos = 0
@mb_matched_size = nil
@mb_last_pos = nil
super
end
|
#terminate Also known as: clear
119 120 121 122 123 124 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 119
def terminate
@mb_pos = string.size
@mb_matched_size = nil
@mb_last_pos = nil
super
end
|
#unscan
127 128 129 130 131 |
# File 'lib/sass/util/multibyte_string_scanner.rb', line 127
def unscan
super
@mb_pos = @mb_last_pos
@mb_last_pos = @mb_matched_size = nil
end
|