Class: String

Inherits:
Object show all
Defined in:
lib/cosmos/core_ext/string.rb,
lib/cosmos/io/json_rpc.rb

Overview

COSMOS specific additions to the Ruby String class

Constant Summary collapse

NON_ASCII_PRINTABLE =
/[^\x21-\x7e\s]/
PRINTABLE_RANGE =

The printable range of ASCII characters

32..126
FLOAT_CHECK_REGEX =

Regular expression to identify a String as a floating point number

/^\s*[-+]?\d*\.\d+\s*$/
SCIENTIFIC_CHECK_REGEX =

Regular expression to identify a String as a floating point number in scientific notation

/^\s*[-+]?(\d+((\.\d+)?)|(\.\d+))[eE][-+]?\d+\s*$/
INT_CHECK_REGEX =

Regular expression to identify a String as an integer

/^\s*[-+]?\d+\s*$/
HEX_CHECK_REGEX =

Regular expression to identify a String as an integer in hexadecimal format

/^\s*0[xX][\dabcdefABCDEF]+\s*$/
ARRAY_CHECK_REGEX =

Regular expression to identify a String as an Array of numbers

/^\s*\[.*\]\s*$/

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/cosmos/io/json_rpc.rb', line 46

def as_json(options = nil)
  if self =~ NON_ASCII_PRINTABLE
    self.to_json_raw_object
  else
    self
  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”).

Parameters:

  • include_extension (Boolean) (defaults to: true)

    Whether to add '.rb' extension

Returns:

  • (String)

    Filename which implements the class name



249
250
251
252
253
254
255
256
257
258
# File 'lib/cosmos/core_ext/string.rb', line 249

def class_name_to_filename(include_extension = true)
  filename = ''
  length = self.length
  length.times do |index|
    filename << '_' if index != 0 and self[index..index] == self[index..index].upcase
    filename << self[index..index].downcase
  end
  filename << '.rb' if include_extension
  filename
end

#convert_to_valueObject

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 not be converted into any of the above then the original String is returned.

Returns:

  • Converts the String into either a Float, Integer, or Array



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cosmos/core_ext/string.rb', line 192

def convert_to_value
  return_value = self
  if 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
  return return_value
end

#filename_to_class_nameString

Converts a String representing a filename (i.e. “my_great_class.rb”) to a Ruby class name (i.e. “MyGreatClass”).

Returns:

  • (String)

    Class name associated with the filename



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/cosmos/core_ext/string.rb', line 264

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.

Parameters:

  • word_size (Integer) (defaults to: 1)

    How many bytes compose a word. Words are grouped together without spaces in between

  • words_per_line (Integer) (defaults to: 16)

    The number of words to display on a single formatted line

  • word_separator (String) (defaults to: ' ')

    The string to place between words

  • indent (Integer) (defaults to: 0)

    The amount of spaces to put in front of each formatted line

  • show_address (Boolean) (defaults to: true)

    Whether to show the hex address of the first byte in the formatted output

  • address_separator (String) (defaults to: ': ')

    The string to put after the hex address. Only used if show_address is true.

  • show_ascii (Boolean) (defaults to: true)

    Whether to interpret the binary data as ASCII characters and display the printable characters to the right of the formatted line

  • ascii_separator (String) (defaults to: ' ')

    The string to put between the formatted line and the ASCII characters. Only used if show_ascii is true.

  • unprintable_character (String) (defaults to: ' ')

    The string to output when data in the binary String does not result in a printable ASCII character. Only used if show_ascii is true.

  • line_separator (String) (defaults to: "\n")

    The string used to end a line. Normaly newline.



54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/cosmos/core_ext/string.rb', line 54

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_stringString

Converts the String representing a hexadecimal number (i.e. “0xABCD”) to a binary String with the same data (i.e “xABxCD”)

Returns:

  • (String)

    Binary byte string



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/cosmos/core_ext/string.rb', line 214

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.

Returns:

  • (Boolean)

    Whether the String represents an Array



183
184
185
# File 'lib/cosmos/core_ext/string.rb', line 183

def is_array?
  if self =~ ARRAY_CHECK_REGEX then true else false end
end

#is_float?Boolean

Returns Whether the String represents a floating point number.

Returns:

  • (Boolean)

    Whether the String represents a floating point number



168
169
170
# File 'lib/cosmos/core_ext/string.rb', line 168

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.

Returns:

  • (Boolean)

    Whether the String represents a hexadecimal number



178
179
180
# File 'lib/cosmos/core_ext/string.rb', line 178

def is_hex?
  if self =~ HEX_CHECK_REGEX then true else false end
end

#is_int?Boolean

Returns Whether the String represents an integer.

Returns:

  • (Boolean)

    Whether the String represents an integer



173
174
175
# File 'lib/cosmos/core_ext/string.rb', line 173

def is_int?
  if self =~ INT_CHECK_REGEX then true else false end
end

#num_linesInteger

Returns The number of lines in the string (as split by the newline character).

Returns:

  • (Integer)

    The number of lines in the string (as split by the newline character)



158
159
160
161
162
# File 'lib/cosmos/core_ext/string.rb', line 158

def num_lines
  value = self.count("\n")
  value += 1 if self[-1..-1] and self[-1..-1] != "\n"
  value
end

#remove_line(line_number, separator = $/) ⇒ String

Uses the String each_line method to interate through the lines and removes the line specified.

Parameters:

  • line_number (Integer)

    The line to remove from the string (1 based)

  • separator (String) (defaults to: $/)

    The record separator to pass to #each_line ($/ by default is the newline character)

Returns:

  • (String)

    A new string with the line removed



146
147
148
149
150
151
152
153
154
# File 'lib/cosmos/core_ext/string.rb', line 146

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_quotesObject

Removes quotes from the given string if present.

"'quoted string'".remove_quotes #=> "quoted string"


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'ext/cosmos/ext/string/string.c', line 19

static VALUE string_remove_quotes(VALUE self)
{
  long length = RSTRING_LEN(self);
  char* ptr = RSTRING_PTR(self);
  char first_char = 0;
  char last_char = 0;

  if (length < 2) {
    return self;
  }

  first_char = ptr[0];
  if ((first_char != 34) && (first_char != 39)) {
    return self;
  }

  last_char = ptr[length - 1];
  if (last_char != first_char) {
    return self;
  }

  return rb_str_new(ptr + 1, length - 2);
}

#simple_formattedObject

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



131
132
133
134
135
136
137
# File 'lib/cosmos/core_ext/string.rb', line 131

def simple_formatted
  string = ''
  self.each_byte do |byte|
    string << sprintf("%02X", byte)
  end
  string
end

#to_classClass

Converts a String representing a class (i.e. “MyGreatClass”) to the actual class that has been required and is present in the Ruby runtime.

Returns:



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/cosmos/core_ext/string.rb', line 287

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 = Cosmos.const_get(self)
    rescue
      begin
        klass = Object.const_get(self)
      rescue
      end
    end
  end
  klass
end