class REXML::Source

Source 可以搜索模式,并包装缓冲区和其他对象,并提供文本的消耗

属性

encoding[R]
line[R]

上次消耗的文本的行号

公共类方法

new(arg, encoding=nil) 点击切换源码

构造函数 @param arg 必须是字符串,并且应该是一个有效的 XML 文档 @param encoding 如果非空,则将源的编码设置为此值,覆盖所有编码检测

# File rexml-3.4.0/lib/rexml/source.rb, line 81
def initialize(arg, encoding=nil)
  @orig = arg
  @scanner = StringScanner.new(@orig)
  if encoding
    self.encoding = encoding
  else
    detect_encoding
  end
  @line = 0
  @encoded_terms = {}
end

公共实例方法

buffer() 点击切换源码

当前缓冲区(接下来要读取的内容)

# File rexml-3.4.0/lib/rexml/source.rb, line 94
def buffer
  @scanner.rest
end
buffer_encoding=(encoding) 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 104
def buffer_encoding=(encoding)
  @scanner.string.force_encoding(encoding)
end
current_line() 点击切换源码

@return 源中的当前行

# File rexml-3.4.0/lib/rexml/source.rb, line 161
def current_line
  lines = @orig.split
  res = lines.grep @scanner.rest[0..30]
  res = res[-1] if res.kind_of? Array
  lines.index( res ) if res
end
drop_parsed_content() 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 98
def drop_parsed_content
  if @scanner.pos > Private::SCANNER_RESET_SIZE
    @scanner.string = @scanner.rest
  end
end
empty?() 点击切换源码

@return 如果 Source 已耗尽,则返回 true

# File rexml-3.4.0/lib/rexml/source.rb, line 156
def empty?
  @scanner.eos?
end
encoding=(enc) 点击切换源码

继承自 Encoding 被覆盖以支持优化的编码/解码

调用超类方法 REXML::Encoding#encoding=
# File rexml-3.4.0/lib/rexml/source.rb, line 110
def encoding=(enc)
  return unless super
  encoding_updated
end
ensure_buffer() 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 128
def ensure_buffer
end
match(pattern, cons=false) 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 131
def match(pattern, cons=false)
  if cons
    @scanner.scan(pattern).nil? ? nil : @scanner
  else
    @scanner.check(pattern).nil? ? nil : @scanner
  end
end
match?(pattern, cons=false) 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 139
def match?(pattern, cons=false)
  if cons
    !@scanner.skip(pattern).nil?
  else
    !@scanner.match?(pattern).nil?
  end
end
position() 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 147
def position
  @scanner.pos
end
position=(pos) 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 151
def position=(pos)
  @scanner.pos = pos
end
read(term = nil) 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 115
def read(term = nil)
end
read_until(term) 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 118
def read_until(term)
  pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
  data = @scanner.scan_until(pattern)
  unless data
    data = @scanner.rest
    @scanner.pos = @scanner.string.bytesize
  end
  data
end

私有实例方法

detect_encoding() 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 170
def detect_encoding
  scanner_encoding = @scanner.rest.encoding
  detected_encoding = "UTF-8"
  begin
    @scanner.string.force_encoding("ASCII-8BIT")
    if @scanner.scan(/\xfe\xff/n)
      detected_encoding = "UTF-16BE"
    elsif @scanner.scan(/\xff\xfe/n)
      detected_encoding = "UTF-16LE"
    elsif @scanner.scan(/\xef\xbb\xbf/n)
      detected_encoding = "UTF-8"
    end
  ensure
    @scanner.string.force_encoding(scanner_encoding)
  end
  self.encoding = detected_encoding
end
encoding_updated() 点击切换源码
# File rexml-3.4.0/lib/rexml/source.rb, line 188
def encoding_updated
  if @encoding != 'UTF-8'
    @scanner.string = decode(@scanner.rest)
    @to_utf = true
  else
    @to_utf = false
    @scanner.string.force_encoding(::Encoding::UTF_8)
  end
end