提交 47f6d37c 编写于 作者: R Rafael Mendonça França

Merge pull request #11606 from vipulnsward/refactor_array_parser

Refactor `ArrayParser`
...@@ -2,6 +2,13 @@ module ActiveRecord ...@@ -2,6 +2,13 @@ module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
class PostgreSQLColumn < Column class PostgreSQLColumn < Column
module ArrayParser module ArrayParser
DOUBLE_QUOTE = '"'
BACKSLASH = "\\"
COMMA = ','
BRACKET_OPEN = '{'
BRACKET_CLOSE = '}'
private private
# Loads pg_array_parser if available. String parsing can be # Loads pg_array_parser if available. String parsing can be
# performed quicker by a native extension, which will not create # performed quicker by a native extension, which will not create
...@@ -12,18 +19,18 @@ module ArrayParser ...@@ -12,18 +19,18 @@ module ArrayParser
include PgArrayParser include PgArrayParser
rescue LoadError rescue LoadError
def parse_pg_array(string) def parse_pg_array(string)
parse_data(string, 0) parse_data(string)
end end
end end
def parse_data(string, index) def parse_data(string)
local_index = index local_index = 0
array = [] array = []
while(local_index < string.length) while(local_index < string.length)
case string[local_index] case string[local_index]
when '{' when BRACKET_OPEN
local_index,array = parse_array_contents(array, string, local_index + 1) local_index,array = parse_array_contents(array, string, local_index + 1)
when '}' when BRACKET_CLOSE
return array return array
end end
local_index += 1 local_index += 1
...@@ -33,9 +40,9 @@ def parse_data(string, index) ...@@ -33,9 +40,9 @@ def parse_data(string, index)
end end
def parse_array_contents(array, string, index) def parse_array_contents(array, string, index)
is_escaping = false is_escaping = false
is_quoted = false is_quoted = false
was_quoted = false was_quoted = false
current_item = '' current_item = ''
local_index = index local_index = index
...@@ -47,29 +54,29 @@ def parse_array_contents(array, string, index) ...@@ -47,29 +54,29 @@ def parse_array_contents(array, string, index)
else else
if is_quoted if is_quoted
case token case token
when '"' when DOUBLE_QUOTE
is_quoted = false is_quoted = false
was_quoted = true was_quoted = true
when "\\" when BACKSLASH
is_escaping = true is_escaping = true
else else
current_item << token current_item << token
end end
else else
case token case token
when "\\" when BACKSLASH
is_escaping = true is_escaping = true
when ',' when COMMA
add_item_to_array(array, current_item, was_quoted) add_item_to_array(array, current_item, was_quoted)
current_item = '' current_item = ''
was_quoted = false was_quoted = false
when '"' when DOUBLE_QUOTE
is_quoted = true is_quoted = true
when '{' when BRACKET_OPEN
internal_items = [] internal_items = []
local_index,internal_items = parse_array_contents(internal_items, string, local_index + 1) local_index,internal_items = parse_array_contents(internal_items, string, local_index + 1)
array.push(internal_items) array.push(internal_items)
when '}' when BRACKET_CLOSE
add_item_to_array(array, current_item, was_quoted) add_item_to_array(array, current_item, was_quoted)
return local_index,array return local_index,array
else else
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册