Gathers utility methods.
Methods
public class
Classes and Modules
Class SDL4R::ParserClass SDL4R::ParserTest
Class SDL4R::SDL4RTest
Class SDL4R::SDLTest
Class SDL4R::SdlBinary
Class SDL4R::SdlParseError
Class SDL4R::SdlTimeSpan
Class SDL4R::Tag
Class SDL4R::TagTest
Constants
MAX_INTEGER_32 | = | 2**31 - 1 |
MIN_INTEGER_32 | = | -(2**31) |
MAX_INTEGER_64 | = | 2**63 - 1 |
MIN_INTEGER_64 | = | -(2**63) |
BASE64_WRAP_LINE_LENGTH | = | 72 |
ESCAPED_QUOTES | = | { "\"" => "\\\"", "'" => "\\'", "`" => "\\`", } |
ESCAPED_CHARS | = | { "\\" => "\\\\", "\t" => "\\t", "\r" => "\\r", "\n" => "\\n", } |
Public class methods
Try to coerce ‘o’ into a SdlBinary. Raise an ArgumentError if it fails.
# File lib/sdl4r/sdl_binary.rb, line 72 72: def self.SdlBinary(o) 73: if o.kind_of? SdlBinary 74: return o 75: elsif o.kind_of? String 76: return SdlBinary.new(o) 77: else 78: raise ArgumentError, "can't coerce argument" 79: end 80: end
Coerce the type to a standard SDL type or raises an ArgumentError.
Returns o if of the following classes: NilClass, String, Numeric, Float, TrueClass, FalseClass, Date, DateTime, Time, SdlTimeSpan, SdlBinary,
Rationals are turned into Floats using Rational#to_f.
# File lib/sdl4r/sdl4r.rb, line 151 151: def self.coerce_or_fail(o) 152: case o 153: 154: when Rational 155: return o.to_f 156: 157: when NilClass, 158: String, 159: Numeric, 160: Float, 161: TrueClass, 162: FalseClass, 163: Date, 164: DateTime, 165: Time, 166: SdlTimeSpan, 167: SdlBinary 168: return o 169: 170: end 171: 172: raise ArgumentError, "#{o.class.name} is not coercible to an SDL type" 173: end
Creates an SDL string representation for a given object and returns it.
o: | the object to format |
add_quotes: | indicates whether quotes will be added to Strings and characters (true by default) |
line_prefix: | the line prefix to use (“” by default) |
indent: | the indent string to use (“t“ by default) |
# File lib/sdl4r/sdl4r.rb, line 44 44: def self.format(o, add_quotes = true, line_prefix = "", indent = "\t") 45: if o.is_a?(String) 46: if add_quotes 47: o_length = 0 48: o.scan(/./m) { o_length += 1 } # counts the number of chars (as opposed of bytes) 49: if o_length == 1 50: return "'" + escape(o, "'") + "'" 51: else 52: return '"' + escape(o, '"') + '"' 53: end 54: else 55: return escape(o) 56: end 57: 58: elsif o.is_a?(Bignum) 59: return o.to_s + "BD" 60: 61: elsif o.is_a?(Integer) 62: if MIN_INTEGER_32 <= o and o <= MAX_INTEGER_32 63: return o.to_s 64: elsif MIN_INTEGER_64 <= o and o <= MAX_INTEGER_64 65: return o.to_s + "L" 66: else 67: return o.to_s + "BD" 68: end 69: 70: elsif o.is_a?(Float) 71: return (o.to_s + "F") 72: 73: elsif o.is_a?(Rational) 74: return o.to_f.to_s + "F" 75: 76: elsif o.is_a?(BigDecimal) 77: s = o.to_s('F') 78: s.sub!(/\.0$/, "") 79: return "#{s}BD" 80: 81: elsif o.nil? 82: return "null" 83: 84: elsif o.is_a?(SdlBinary) 85: encoded_o = Base64.encode64(o.bytes) 86: encoded_o.gsub!(/[\r\n]/m, "") # Remove the EOL inserted every 60 chars 87: 88: if add_quotes 89: if encoded_o.length > BASE64_WRAP_LINE_LENGTH 90: # FIXME: we should a constant or some parameter instead of hardcoded spaces 91: wrap_lines_in_ascii(encoded_o, BASE64_WRAP_LINE_LENGTH, "#{line_prefix}#{indent}") 92: encoded_o.insert(0, "[#{$/}") 93: encoded_o << "#{$/}#{line_prefix}]" 94: else 95: encoded_o.insert(0, "[") 96: encoded_o << "]" 97: end 98: end 99: 100: return encoded_o 101: 102: # Below, we use "#{o.year}" instead of "%Y" because "%Y" always emit 4 chars at least even if 103: # the date is before 1000. 104: elsif o.is_a?(DateTime) || o.is_a?(Time) 105: milliseconds = get_datetime_milliseconds(o) 106: 107: if milliseconds == 0 108: if o.zone 109: return o.strftime("#{o.year}/%m/%d %H:%M:%S%Z") 110: else 111: return o.strftime("#{o.year}/%m/%d %H:%M:%S") 112: end 113: else 114: if o.zone 115: return o.strftime("#{o.year}/%m/%d %H:%M:%S." + milliseconds.to_s.ljust(3, '0') + "%Z") 116: else 117: return o.strftime("#{o.year}/%m/%d %H:%M:%S." + milliseconds.to_s.ljust(3, '0')) 118: end 119: end 120: 121: elsif o.is_a?(Date) 122: return o.strftime("#{o.year}/%m/%d") 123: 124: else 125: return o.to_s 126: end 127: end
Creates and returns the object representing a datetime (DateTime in the default implementation). This method is, by default, called by the Parser class. It could be overriden as follows in order to get Time instances from all the SDL4R parsers.
module SDL4R def self.new_date_time(year, month, day, hour, min, sec, time_zone_offset) Time.utc(year, month, day, hour, min, sec) end end
# File lib/sdl4r/sdl4r.rb, line 139 139: def self.new_date_time(year, month, day, hour, min, sec, time_zone_offset) 140: DateTime.civil(year, month, day, hour, min, sec, time_zone_offset) 141: end
Creates and returns a tag named “root” and add all the tags specified in the given input.
input: | String, IO, Pathname or URI. |
root = SDL4R::read(<<EOF planets { earth area_km2=510900000 mars } EOF ) root = SDL4R::read(Pathname.new("my_dir/my_file.sdl")) IO.open("my_dir/my_file.sdl", "r") { |io| root = SDL4R::read(io) } root = SDL4R::read(URI.new("http://my_site/my_file.sdl"))
# File lib/sdl4r/sdl4r.rb, line 232 232: def self.read(input) 233: Tag.new("root").read(input) 234: 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"); # { "value" => 1, "debugging" => true, "time" => SdlTimeSpan.new(12, 24, 01) }
# File lib/sdl4r/sdl4r.rb, line 270 270: def self.to_attribute_map(s) 271: raise ArgumentError, "'s' cannot be null" if s.nil? 272: return read("atts " + s).child.attributes 273: end
Parses and returns the value corresponding with the specified SDL literal.
SDL4R.to_value("\"abcd\"") # => "abcd" SDL4R.to_value("1") # => 1 SDL4R.to_value("null") # => nil
# File lib/sdl4r/sdl4r.rb, line 242 242: def self.to_value(s) 243: raise ArgumentError, "'s' cannot be null" if s.nil? 244: return read(s).child.value 245: 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_value_array("1 true 12:24:01")
Will return an int, a boolean, and a time span.
# File lib/sdl4r/sdl4r.rb, line 256 256: def self.to_value_array(s) 257: raise ArgumentError, "'s' cannot be null" if s.nil? 258: return read(s).child.values 259: end
Validates an SDL identifier String. SDL Identifiers must start with a Unicode letter or underscore (_) and contain only unicode letters, digits, underscores (_), dashes(-) and periods (.).
Raises
ArgumentError if the identifier is not legal
TODO: support UTF-8 identifiers
# File lib/sdl4r/sdl4r.rb, line 184 184: def self.validate_identifier(identifier) 185: if identifier.nil? or identifier.empty? 186: raise ArgumentError, "SDL identifiers cannot be null or empty." 187: end 188: 189: # in Java, was if(!Character.isJavaIdentifierStart(identifier.charAt(0))) 190: unless identifier =~ /^[a-zA-Z_]/ 191: raise ArgumentError, 192: "'" + identifier[0..0] + 193: "' is not a legal first character for an SDL identifier. " + 194: "SDL Identifiers must start with a unicode letter or " + 195: "an underscore (_)." 196: end 197: 198: unless identifier.length == 1 or identifier =~ /^[a-zA-Z_][a-zA-Z_0-9\-\.]*$/ 199: for i in 1..identifier.length 200: unless identifier[i..i] =~ /^[a-zA-Z_0-9\-]$/ 201: raise ArgumentError, 202: "'" + identifier[i..i] + 203: "' is not a legal character for an SDL identifier. " + 204: "SDL Identifiers must start with a unicode letter or " + 205: "underscore (_) followed by 0 or more unicode " + 206: "letters, digits, underscores (_), or dashes (-)" 207: end 208: end 209: end 210: end