lib/sdl4r/sdl.rb in sdl4r-0.9.1 vs lib/sdl4r/sdl.rb in sdl4r-0.9.2
- old
+ new
@@ -20,26 +20,25 @@
require 'date'
# Various SDL related utility methods
#
module SDL4R
+
+ # Creates and returns a tag named "root" and add all the tags specified in the given +input+.
+ #
+ # +input+:: String, IO, Pathname or URI.
+ #
+ def self.read(input)
+ Tag.new("root").read(input)
+ end
MAX_INTEGER_32 = 2**31 - 1
MIN_INTEGER_32 = -(2**31)
MAX_INTEGER_64 = 2**63 - 1
MIN_INTEGER_64 = -(2**63)
-
- # Returns the milliseonds part of a DateTime (by translating the +sec_fraction+ part) as an
- # integer.
- def self.get_date_milliseconds(date)
- sec_fraction = date.sec_fraction() # in days
- # 86400000 is the number of milliseconds in a day
- return (sec_fraction * 86400000).round()
- end
-
BASE64_WRAP_LINE_LENGTH = 72
# Creates an SDL string representation for a given object and returns it.
#
# +o+:: the object to format
@@ -128,77 +127,11 @@
else
return o.to_s
end
end
-
- # Wraps lines in "s" (by modifying it). This method only supports 1-byte character strings.
- #
- def self.wrap_lines_in_ascii(s, line_length, line_prefix = nil)
- # We could use such code if it supported any value for "line_prefix": unfortunately it is capped
- # at 64 in the regular expressions.
- #
- # return "#{line_prefix}" + encoded_o.scan(/.{1,#{line_prefix}}/).join("#{$/}#{line_prefix}")
-
- eol_size = "#{$/}".size
-
- i = 0
- while i < s.size
- if i > 0
- s.insert(i, $/)
- i += eol_size
- end
-
- if line_prefix
- s.insert(i, line_prefix)
- i += line_prefix.size
- end
-
- i += line_length
- end
- end
-
- ESCAPED_QUOTES = {
- "\"" => "\\\"",
- "'" => "\\'",
- "`" => "\\`",
- }
-
- ESCAPED_CHARS = {
- "\\" => "\\\\",
- "\t" => "\\t",
- "\r" => "\\r",
- "\n" => "\\n",
- }
- ESCAPED_CHARS.merge!(ESCAPED_QUOTES)
-
- # Returns an escaped version of +s+ (i.e. where characters which need to be
- # escaped, are escaped).
- #
- def self.escape(s, quote_char = nil)
- escaped_s = ""
-
- s.each_char { |c|
- escaped_char = ESCAPED_CHARS[c]
- if escaped_char
- if ESCAPED_QUOTES.has_key?(c)
- if quote_char && c == quote_char
- escaped_s << escaped_char
- else
- escaped_s << c
- end
- else
- escaped_s << escaped_char
- end
- else
- escaped_s << c
- end
- }
- return escaped_s
- end
-
# This method was kept from the Java code but it is not sure if it should have a usefulness yet.
#
def self.coerce_or_fail(o)
return o
end
@@ -224,11 +157,11 @@
"' is not a legal first character for an SDL identifier. " +
"SDL Identifiers must start with a unicode letter or " +
"an underscore (_)."
end
- unless identifier.length == 1 or identifier =~ /^[a-zA-Z_][a-zA-Z_0-9\-]*$/
+ unless identifier.length == 1 or identifier =~ /^[a-zA-Z_][a-zA-Z_0-9\-\.]*$/
for i in 1..identifier.length
unless identifier[i..i] =~ /^[a-zA-Z_0-9\-]$/
raise ArgumentError,
"'" + identifier[i..i] +
"' is not a legal character for an SDL identifier. " +
@@ -237,6 +170,123 @@
"letters, digits, underscores (_), or dashes (-)"
end
end
end
end
+
+ # Parses and returns the value corresponding with the specified SDL literal.
+ #
+ def self.to_value(s)
+ raise ArgumentError, "'s' cannot be null" if s.nil?
+ return read(s).child.value
+ end
+
+ # Parse the string of values and return a list. The string is handled
+ # as if it is the values portion of an SDL tag.
+ #
+ # Example
+ #
+ # array = SDL4R.to_values("1 true 12:24:01")
+ #
+ # Will return an int, a boolean, and a time span.
+ #
+ def self.to_value_array(s)
+ raise ArgumentError, "'s' cannot be null" if s.nil?
+ return read(s).child.values
+ end
+
+ # Parse a string representing the attributes portion of an SDL tag
+ # and return the results as a map.
+ #
+ # Example
+ #
+ # hash = SDL4R.to_attribute_hash("value=1 debugging=on time=12:24:01");
+ #
+ # Will return a map containing value=1, debugging=true, and time=12:24:01
+ #
+ def self.to_attribute_map(s)
+ raise ArgumentError, "'s' cannot be null" if s.nil?
+ return read("atts " + s).child.attributes
+ end
+
+ # The following is a not so readable way to implement module private methods in Ruby: we add
+ # private methods to the singleton class of +self+ i.e. the SDL4R module.
+ class << self
+ private
+
+ # Wraps lines in "s" (by modifying it). This method only supports 1-byte character strings.
+ #
+ def wrap_lines_in_ascii(s, line_length, line_prefix = nil)
+ # We could use such code if it supported any value for "line_prefix": unfortunately it is capped
+ # at 64 in the regular expressions.
+ #
+ # return "#{line_prefix}" + encoded_o.scan(/.{1,#{line_prefix}}/).join("#{$/}#{line_prefix}")
+
+ eol_size = "#{$/}".size
+
+ i = 0
+ while i < s.size
+ if i > 0
+ s.insert(i, $/)
+ i += eol_size
+ end
+
+ if line_prefix
+ s.insert(i, line_prefix)
+ i += line_prefix.size
+ end
+
+ i += line_length
+ end
+ end
+
+ ESCAPED_QUOTES = {
+ "\"" => "\\\"",
+ "'" => "\\'",
+ "`" => "\\`",
+ }
+
+ ESCAPED_CHARS = {
+ "\\" => "\\\\",
+ "\t" => "\\t",
+ "\r" => "\\r",
+ "\n" => "\\n",
+ }
+ ESCAPED_CHARS.merge!(ESCAPED_QUOTES)
+
+ # Returns an escaped version of +s+ (i.e. where characters which need to be
+ # escaped, are escaped).
+ #
+ def escape(s, quote_char = nil)
+ escaped_s = ""
+
+ s.each_char { |c|
+ escaped_char = ESCAPED_CHARS[c]
+ if escaped_char
+ if ESCAPED_QUOTES.has_key?(c)
+ if quote_char && c == quote_char
+ escaped_s << escaped_char
+ else
+ escaped_s << c
+ end
+ else
+ escaped_s << escaped_char
+ end
+ else
+ escaped_s << c
+ end
+ }
+
+ return escaped_s
+ end
+
+ # Returns the milliseonds part of a DateTime (by translating the +sec_fraction+ part) as an
+ # integer.
+ def get_date_milliseconds(date)
+ sec_fraction = date.sec_fraction() # in days
+ # 86400000 is the number of milliseconds in a day
+ return (sec_fraction * 86400000).round()
+ end
+
+ end
+
end
\ No newline at end of file