Class: String
- Defined in:
- lib/openc3/core_ext/string.rb,
lib/openc3/io/json_rpc.rb
Overview
OpenC3 specific additions to the Ruby String class
Constant Summary collapse
- NON_ASCII_PRINTABLE =
/[^\x21-\x7e\s]/
- NON_UTF8_PRINTABLE =
/[\x00-\x08\x0E-\x1F\x7F]/
- PRINTABLE_RANGE =
The printable range of ASCII characters
32..126
- NON_PRINTABLE_REGEX =
Regular expression to identify a character that is not in the printable range
/[^\s!-~]/
- FLOAT_CHECK_REGEX =
Regular expression to identify a String as a floating point number
/\A\s*[-+]?\d*\.\d+\s*\z/
- SCIENTIFIC_CHECK_REGEX =
Regular expression to identify a String as a floating point number in scientific notation
/\A\s*[-+]?(\d+((\.\d+)?)|(\.\d+))[eE][-+]?\d+\s*\z/
- INT_CHECK_REGEX =
Regular expression to identify a String as an integer
/\A\s*[-+]?\d+\s*\z/
- HEX_CHECK_REGEX =
Regular expression to identify a String as an integer in hexadecimal format
/\A\s*0[xX][\dabcdefABCDEF]+\s*\z/
- ARRAY_CHECK_REGEX =
Regular expression to identify a String as an Array of numbers
/\A\s*\[.*\]\s*\z/
Instance Method Summary collapse
- #as_json(_options = nil) ⇒ Object
-
#class_name_to_filename(include_extension = true) ⇒ String
Converts a String representing a class (i.e. “MyGreatClass”) to a Ruby filename which implements the class (i.e. “my_great_class.rb”).
- #comment_erb ⇒ Object
-
#convert_to_value ⇒ Object
depending on what the String represents.
-
#filename_to_class_name ⇒ String
Converts a String representing a filename (i.e. “my_great_class.rb”) to a Ruby class name (i.e. “MyGreatClass”).
-
#formatted(word_size = 1, words_per_line = 16, word_separator = ' ', indent = 0, show_address = true, address_separator = ': ', show_ascii = true, ascii_separator = ' ', unprintable_character = ' ', line_separator = "\n") ⇒ Object
Displays a String containing binary data in a human readable format by converting each byte to the hex representation.
-
#hex_to_byte_string ⇒ String
Converts the String representing a hexadecimal number (i.e. “0xABCD”) to a binary String with the same data (i.e “xABxCD”).
-
#is_array? ⇒ Boolean
Whether the String represents an Array.
-
#is_float? ⇒ Boolean
Whether the String represents a floating point number.
-
#is_hex? ⇒ Boolean
Whether the String represents a hexadecimal number.
-
#is_int? ⇒ Boolean
Whether the String represents an integer.
-
#is_printable? ⇒ Boolean
Whether the string contains only printable characters.
-
#num_lines ⇒ Integer
The number of lines in the string (as split by the newline character).
-
#quote_if_necessary(quote_char = '"') ⇒ String
Adds quotes if the string contains whitespace.
-
#remove_line(line_number, separator = $/) ⇒ String
Uses the String each_line method to iterate through the lines and removes the line specified.
-
#remove_quotes ⇒ Object
Removes quotes from the given string if present.
-
#simple_formatted ⇒ Object
Displays a String containing binary data in a human readable format by converting each byte to the hex representation.
-
#to_class ⇒ Class
Converts a String representing a class (i.e. “MyGreatClass”) to the actual class that has been required and is present in the Ruby runtime.
-
#to_utf8 ⇒ String
Converts a string to UTF-8 and returns a new string Assumes the string is Windows-1252 encoded if marked ASCII-8BIT and not UTF-8 compatible.
-
#to_utf8! ⇒ String
Converts a string to UTF-8 in place Assumes the string is Windows-1252 encoded if marked ASCII-8BIT and not UTF-8 compatible.
Instance Method Details
#as_json(_options = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/openc3/io/json_rpc.rb', line 60 def as_json( = nil) as_utf8 = self.dup.force_encoding('UTF-8') if as_utf8.valid_encoding? if as_utf8 =~ NON_UTF8_PRINTABLE return self.to_json_raw_object else return as_utf8 end else return self.to_json_raw_object end end |
#class_name_to_filename(include_extension = true) ⇒ String
Converts a String representing a class (i.e. “MyGreatClass”) to a Ruby filename which implements the class (i.e. “my_great_class.rb”).
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/openc3/core_ext/string.rb', line 290 def class_name_to_filename(include_extension = true) string = self.split("::")[-1] # Remove any namespacing filename = '' length = string.length length.times do |index| filename << '_' if index != 0 and string[index..index] == string[index..index].upcase filename << string[index..index].downcase end filename << '.rb' if include_extension filename end |
#comment_erb ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/openc3/core_ext/string.rb', line 392 def comment_erb output = self.lines.collect! do |line| # If we have a commented out line that starts with # # but not followed by % (allows for disabling ERB comments), # which contains an ERB statement (<% ...) # then comment out the ERB statement (<%# ...). # We explicitly don't comment out trailing ERB statements # as that is not typical and is difficult to regex if line =~ /^\s*#[^%]*<%/ line.gsub!('<%', '<%#') end line end return output.join("") end |
#convert_to_value ⇒ Object
depending on what the String represents. It can successfully convert floating point numbers in both fixed and scientific notation, integers in hexadecimal notation, and Arrays. If it can’t be converted into any of the above then the original String is returned.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/openc3/core_ext/string.rb', line 222 def convert_to_value return_value = self begin upcase_self = self.upcase if upcase_self == 'INFINITY'.freeze return_value = Float::INFINITY elsif upcase_self == '-INFINITY'.freeze return_value = -Float::INFINITY elsif upcase_self == 'NAN'.freeze return_value = Float::NAN elsif self.is_float? # Floating Point in normal or scientific notation return_value = self.to_f elsif self.is_int? # Integer return_value = self.to_i elsif self.is_hex? # Hex return_value = Integer(self) elsif self.is_array? # Array return_value = eval(self) end rescue Exception # Something went wrong so just return the string as is end return return_value end |
#filename_to_class_name ⇒ String
Converts a String representing a filename (i.e. “my_great_class.rb”) to a Ruby class name (i.e. “MyGreatClass”).
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/openc3/core_ext/string.rb', line 306 def filename_to_class_name filename = File.basename(self) class_name = '' length = filename.length upcase_next = true length.times do |index| break if filename[index..index] == '.' if filename[index..index] == '_' upcase_next = true elsif upcase_next class_name << filename[index..index].upcase upcase_next = false else class_name << filename[index..index].downcase end end class_name end |
#formatted(word_size = 1, words_per_line = 16, word_separator = ' ', indent = 0, show_address = true, address_separator = ': ', show_ascii = true, ascii_separator = ' ', unprintable_character = ' ', line_separator = "\n") ⇒ Object
Displays a String containing binary data in a human readable format by converting each byte to the hex representation.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/openc3/core_ext/string.rb', line 67 def formatted( word_size = 1, words_per_line = 16, word_separator = ' ', indent = 0, show_address = true, address_separator = ': ', show_ascii = true, ascii_separator = ' ', unprintable_character = ' ', line_separator = "\n" ) string = '' byte_offset = 0 bytes_per_line = word_size * words_per_line indent_string = ' ' * indent ascii_line = '' self.each_byte do |byte| if byte_offset % bytes_per_line == 0 # Create the indentation at the beginning of each line string << indent_string # Add the address if requested string << sprintf("%08X%s", byte_offset, address_separator) if show_address end # Add the byte string << sprintf("%02X", byte) # Create the ASCII representation if requested if show_ascii if PRINTABLE_RANGE.include?(byte) ascii_line << [byte].pack('C') else ascii_line << unprintable_character end end # Move to next byte byte_offset += 1 # If we're at the end of the line we output the ascii if requested if byte_offset % bytes_per_line == 0 if show_ascii string << "#{ascii_separator}#{ascii_line}" ascii_line = '' end string << line_separator # If we're at a word junction then output the word_separator elsif (byte_offset % word_size == 0) and byte_offset != self.length string << word_separator end end # We're done printing all the bytes. Now check to see if we ended in the # middle of a line. If so we have to print out the final ASCII if # requested. if byte_offset % bytes_per_line != 0 if show_ascii num_word_separators = ((byte_offset % bytes_per_line) - 1) / word_size existing_length = (num_word_separators * word_separator.length) + ((byte_offset % bytes_per_line) * 2) full_line_length = (bytes_per_line * 2) + ((words_per_line - 1) * word_separator.length) filler = ' ' * (full_line_length - existing_length) ascii_filler = ' ' * (bytes_per_line - ascii_line.length) string << "#{filler}#{ascii_separator}#{ascii_line}#{ascii_filler}" ascii_line = '' end string << line_separator end string end |
#hex_to_byte_string ⇒ String
Converts the String representing a hexadecimal number (i.e. “0xABCD”) to a binary String with the same data (i.e “xABxCD”)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/openc3/core_ext/string.rb', line 255 def hex_to_byte_string string = self.dup # Remove leading 0x or 0X if string[0..1] == '0x' or string[0..1] == '0X' string = string[2..-1] end length = string.length length += 1 unless (length % 2) == 0 array = [] (length / 2).times do # Grab last two characters if string.length >= 2 last_two_characters = string[-2..-1] string = string[0..-3] else last_two_characters = string[0..0] string = '' end int_value = Integer('0x' + last_two_characters) array.unshift(int_value) end array.pack("C*") end |
#is_array? ⇒ Boolean
Returns Whether the String represents an Array.
208 209 210 |
# File 'lib/openc3/core_ext/string.rb', line 208 def is_array? if ARRAY_CHECK_REGEX.match?(self) then true else false end end |
#is_float? ⇒ Boolean
Returns Whether the String represents a floating point number.
193 194 195 |
# File 'lib/openc3/core_ext/string.rb', line 193 def is_float? if self =~ FLOAT_CHECK_REGEX or self =~ SCIENTIFIC_CHECK_REGEX then true else false end end |
#is_hex? ⇒ Boolean
Returns Whether the String represents a hexadecimal number.
203 204 205 |
# File 'lib/openc3/core_ext/string.rb', line 203 def is_hex? if HEX_CHECK_REGEX.match?(self) then true else false end end |
#is_int? ⇒ Boolean
Returns Whether the String represents an integer.
198 199 200 |
# File 'lib/openc3/core_ext/string.rb', line 198 def is_int? if INT_CHECK_REGEX.match?(self) then true else false end end |
#is_printable? ⇒ Boolean
Returns Whether the string contains only printable characters.
213 214 215 |
# File 'lib/openc3/core_ext/string.rb', line 213 def is_printable? if NON_PRINTABLE_REGEX.match?(self) then false else true end end |
#num_lines ⇒ Integer
Returns The number of lines in the string (as split by the newline character).
171 172 173 174 175 |
# File 'lib/openc3/core_ext/string.rb', line 171 def num_lines value = self.count("\n") value += 1 if self[-1..-1] and self[-1..-1] != "\n" value end |
#quote_if_necessary(quote_char = '"') ⇒ String
Adds quotes if the string contains whitespace
358 359 360 361 362 363 364 |
# File 'lib/openc3/core_ext/string.rb', line 358 def quote_if_necessary(quote_char = '"') if /\s/.match?(self) return quote_char + self + quote_char else return self end end |
#remove_line(line_number, separator = $/) ⇒ String
Uses the String each_line method to iterate through the lines and removes the line specified.
159 160 161 162 163 164 165 166 167 |
# File 'lib/openc3/core_ext/string.rb', line 159 def remove_line(line_number, separator = $/) new_string = '' index = 1 self.each_line(separator) do |line| new_string << line unless index == line_number index += 1 end new_string end |
#remove_quotes ⇒ Object
Removes quotes from the given string if present.
"'quoted string'".remove_quotes #=> "quoted string"
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/openc3/core_ext/string.rb', line 179 def remove_quotes return self if self.length < 2 first_char = self[0] return self if (first_char != '"') && (first_char != "'") last_char = self[-1] return self if first_char != last_char return self[1..-2] end |
#simple_formatted ⇒ Object
Displays a String containing binary data in a human readable format by converting each byte to the hex representation. Simply formatted as a single string of bytes
144 145 146 147 148 149 150 |
# File 'lib/openc3/core_ext/string.rb', line 144 def simple_formatted string = '' self.each_byte do |byte| string << sprintf("%02X", byte) end string end |
#to_class ⇒ Class
Converts a String representing a class (i.e. “MyGreatClass”) to the actual class that has been required and is present in the Ruby runtime.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/openc3/core_ext/string.rb', line 330 def to_class klass = nil split_self = self.split('::') if split_self.length > 1 split_self.each do |class_name| if klass klass = klass.const_get(class_name) else klass = Object.const_get(class_name) end end else begin klass = OpenC3.const_get(self) rescue begin klass = Object.const_get(self) rescue end end end klass end |
#to_utf8 ⇒ String
Converts a string to UTF-8 and returns a new string Assumes the string is Windows-1252 encoded if marked ASCII-8BIT and not UTF-8 compatible
370 371 372 |
# File 'lib/openc3/core_ext/string.rb', line 370 def to_utf8 self.dup.to_utf8! end |
#to_utf8! ⇒ String
Converts a string to UTF-8 in place Assumes the string is Windows-1252 encoded if marked ASCII-8BIT and not UTF-8 compatible
378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/openc3/core_ext/string.rb', line 378 def to_utf8! if self.encoding == Encoding::ASCII_8BIT if self.force_encoding('UTF-8').valid_encoding? return self else # Note: this will replace any characters without a valid conversion with space (shouldn't be possible from Windows-1252) return self.force_encoding('Windows-1252').encode!('UTF-8', invalid: :replace, undef: :replace, replace: ' ') end else # Note: this will replace any characters without a valid conversion with space self.encode!('UTF-8', invalid: :replace, undef: :replace, replace: ' ') end end |