Class: String

Inherits:
Object show all
Defined in:
lib/origen/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#camel_caseObject



27
28
29
30
# File 'lib/origen/core_ext/string.rb', line 27

def camel_case
  Origen.deprecate "String#camel_case! is deprecated, use String#camelcase instead, or if you want to get rid of spaces: my_string.gsub(' ', '_').camelcase"
  gsub(/\s+/, '_').split('_').map(&:capitalize).join
end

#escape_underscoresObject Also known as: escape_underscore



22
23
24
# File 'lib/origen/core_ext/string.rb', line 22

def escape_underscores
  gsub('_', '\_')
end

#excel_col_indexObject Also known as: xls_col_index, xlsx_col_index, spreadsheet_col_index

Convert Excel/Spreadsheet column to integer



159
160
161
162
163
# File 'lib/origen/core_ext/string.rb', line 159

def excel_col_index
  str = split('').map(&:upcase).join('')
  offset = 'A'.ord - 1
  str.chars.inject(0) { |x, c| x * 26 + c.ord - offset }
end

#is_downcase?Boolean Also known as: is_lowercase?

Boolean if the string is uppercase Will not work with odd character sets

Returns:

  • (Boolean)


153
154
155
# File 'lib/origen/core_ext/string.rb', line 153

def is_downcase?
  self == downcase
end

#is_numeric?Boolean Also known as: numeric?

Check if a String is a numeric

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/origen/core_ext/string.rb', line 103

def is_numeric?
  return true if self =~ /\A\d+\Z/
  true if Float(self) rescue false # rubocop:disable Style/RescueModifier
end

#is_upcase?Boolean Also known as: is_uppercase?

Boolean if the string is uppercase Will not work with odd character sets

Returns:

  • (Boolean)


146
147
148
# File 'lib/origen/core_ext/string.rb', line 146

def is_upcase?
  self == upcase
end

#is_verilog_number?Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
# File 'lib/origen/core_ext/string.rb', line 135

def is_verilog_number?
  case self
  when /^[b,o,d,h]\S+$/, /^\d+\'[b,o,d,h]\S+$/, /^\d+\'s[b,o,d,h]\S+$/
    true
  else
    false
  end
end

#pad_leading_zeros(width) ⇒ Object



32
33
34
35
36
# File 'lib/origen/core_ext/string.rb', line 32

def pad_leading_zeros(width)
  str = self
  (0..(width - size) - 1).each { str = '0' + str }
  str
end

#squeeze_linesObject



87
88
89
# File 'lib/origen/core_ext/string.rb', line 87

def squeeze_lines
  split(/\n+/).join(' ').squeeze(' ')
end

#symbolizeObject

Sanitizes the string for conversion to a symbol and returns a lower cased symbol version of the string



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/origen/core_ext/string.rb', line 57

def symbolize
  orig_had_leading_underscore = match(/^\_/) ? true : false
  orig_had_trailing_underscore = match(/\_$/) ? true : false
  new_str = gsub(/(\?|\!|\-|\/|\\|\n|\s|\(|\)|\.|\[|\]|-|{|})/, '_').downcase
  # Get rid of adjacent underscores
  new_str.match(/\_\_/) ? new_str = new_str.squeeze('_') : new_str
  new_str.chomp!('_') unless orig_had_trailing_underscore
  unless orig_had_leading_underscore
    new_str = new_str[1..-1] if new_str.match(/^\_/)
  end
  @@symbolize ||= {}
  @@symbolize[self] ||= new_str.to_sym
end

#titleize(options = {}) ⇒ Object

Capitalize every word



124
125
126
127
128
129
130
131
132
133
# File 'lib/origen/core_ext/string.rb', line 124

def titleize(options = {})
  options = {
    keep_specials: false
  }.update(options)
  if options[:keep_specials]
    split.map(&:capitalize).join(' ')
  else
    split(/ |\_|\-/).map(&:capitalize).join(' ')
  end
end

#to_boolObject

Attempt to convert a String to a boolean answer



92
93
94
95
96
97
98
99
100
# File 'lib/origen/core_ext/string.rb', line 92

def to_bool
  if self == true || self =~ (/^(true|t|yes|y|1)$/i)
    return true
  elsif self == false || self.empty? || self =~ (/^(false|f|no|n|0)$/i)
    return false
  else
    return nil
  end
end

#to_decObject



12
13
14
15
16
17
18
19
20
# File 'lib/origen/core_ext/string.rb', line 12

def to_dec
  if self.is_verilog_number?
    verilog_to_dec
  elsif match(/^0[x,o,d,b]\S+/)
    _to_dec(self)
  else
    to_i
  end
end

#to_lines(length) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/origen/core_ext/string.rb', line 38

def to_lines(length)
  lines = []
  line = []
  len = 0
  split(/\s+/).each do |word|
    if (len + word.length) > length
      lines << line.join(' ')
      line = []
      len = 0
    end
    line << word
    len += word.length + 1 # For the trailing space
  end
  lines << line.join(' ') unless line.empty?
  lines
end

#to_numericObject Also known as: to_number

Convert the String to a Numeric (Float or Integer)



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/origen/core_ext/string.rb', line 110

def to_numeric
  if self.numeric?
    if to_i == to_f
      to_i
    else
      to_f
    end
  else
    fail "'#{self}' cannot be converted to a Numeric, exiting..."
  end
end

#to_snakecaseObject Also known as: snakecase



81
82
83
84
# File 'lib/origen/core_ext/string.rb', line 81

def to_snakecase
  Origen.deprecate 'String#to_snakecase is deprecated, use String#underscore instead since it is aware of FSL acronyms'
  dup.tap(&:to_snakecase!)
end

#to_snakecase!Object Also known as: snakecase!

acronyms



72
73
74
75
76
77
78
# File 'lib/origen/core_ext/string.rb', line 72

def to_snakecase!
  Origen.deprecate 'String#to_snakecase! is deprecated, use String#underscore instead since it is aware of FSL acronyms'
  gsub!(/\s+/, '')
  g = gsub!(/(.)([A-Z])/, '\1_\2');
  d = downcase!
  g || d
end