Class: String
Direct Known Subclasses
Origen::Utility::FileDiff::OutputFile::Line, Origen::VersionString
Instance Method Summary collapse
- #camel_case ⇒ Object
- #escape_underscores(smartly = false) ⇒ Object (also: #escape_underscore)
-
#excel_col_index ⇒ Object
(also: #xls_col_index, #xlsx_col_index, #spreadsheet_col_index)
Convert Excel/Spreadsheet column to integer.
-
#is_downcase? ⇒ Boolean
(also: #is_lowercase?)
Boolean if the string is uppercase Will not work with odd character sets.
-
#is_numeric? ⇒ Boolean
(also: #numeric?)
Check if a String is a numeric.
-
#is_upcase? ⇒ Boolean
(also: #is_uppercase?)
Boolean if the string is uppercase Will not work with odd character sets.
- #is_verilog_number? ⇒ Boolean
- #match_all(regex) ⇒ Object
- #pad_leading_zeros(width) ⇒ Object
- #squeeze_lines ⇒ Object
-
#symbolize ⇒ Object
Sanitizes the string for conversion to a symbol and returns a lower cased symbol version of the string.
-
#titleize(options = {}) ⇒ Object
Capitalize every word.
-
#to_bool ⇒ Object
Attempt to convert a String to a boolean answer.
- #to_dec ⇒ Object
- #to_lines(length) ⇒ Object
-
#to_numeric ⇒ Object
(also: #to_number)
Convert the String to a Numeric (Float or Integer).
- #to_snakecase ⇒ Object (also: #snakecase)
-
#to_snakecase! ⇒ Object
(also: #snakecase!)
acronyms.
Instance Method Details
#camel_case ⇒ Object
102 103 104 105 |
# File 'lib/origen/core_ext/string.rb', line 102 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_underscores(smartly = false) ⇒ Object Also known as: escape_underscore
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 |
# File 'lib/origen/core_ext/string.rb', line 23 def escape_underscores(smartly = false) if smartly # Need to make sub-string ranges where the URLs are located urls = URI.extract(self, %w(http https ssh)) url_matches = [].tap do |new_ary| urls.each do |url| scan(/#{Regexp.escape(url)}/) do |c| indices = ($LAST_MATCH_INFO.offset(0).first..($LAST_MATCH_INFO.offset(0).first + c.size - 1)) new_ary << [indices, c] end end end # This order is because a better single regex is not yet found italic_regex = [/^(\_\w+\_)\s+/, /(\_\w+\_)$/, /\s*(\_\w+\_)\s+/] italic_matches = [].tap do |new_ary| italic_regex.each_with_index do |regex, i| scan(regex) do |c| offset = c.first.size - 1 if i == 0 indices = (0..offset) elsif i == 1 indices = (size - 1 - offset..size - 1) else indices = ($LAST_MATCH_INFO.offset(0).first..($LAST_MATCH_INFO.offset(0).first + c.first.size - 1)) end new_ary << [indices, c] end end end # Make sure there are no italic matches within the URL ranges filtered_italic_matches = [].tap do |new_ary| url_matches.each do |url_ary| url_range = url_ary.first italic_matches.each do |italic_ary| italic_range = italic_ary.first # Ruby 2.6 has the Range#cover method but until then unless ranges_overlap?(url_range, italic_range) new_ary << italic_ary unless new_ary.include?(italic_ary) end end end end italic_ranges = [].tap do |new_ary| filtered_italic_ranges = filtered_italic_matches.map(&:first) italic_range_mins = filtered_italic_matches.map(&:first).map(&:min).sort italic_range_mins.each_with_index do |range_min, i| filtered_italic_ranges.each do |r| new_ary << r if r.min == range_min end end end str = dup inc = 0 italic_ranges.each do |r| str[r.first + inc] = '\_' inc += 1 str[r.last + inc] = '\_' inc += 1 end str else gsub('_', '\_') end end |
#excel_col_index ⇒ Object Also known as: xls_col_index, xlsx_col_index, spreadsheet_col_index
Convert Excel/Spreadsheet column to integer
233 234 235 236 237 |
# File 'lib/origen/core_ext/string.rb', line 233 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
227 228 229 |
# File 'lib/origen/core_ext/string.rb', line 227 def is_downcase? self == downcase end |
#is_numeric? ⇒ Boolean Also known as: numeric?
Check if a String is a numeric
176 177 178 179 180 |
# File 'lib/origen/core_ext/string.rb', line 176 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
220 221 222 |
# File 'lib/origen/core_ext/string.rb', line 220 def is_upcase? self == upcase end |
#is_verilog_number? ⇒ Boolean
209 210 211 212 213 214 215 216 |
# File 'lib/origen/core_ext/string.rb', line 209 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 |
#match_all(regex) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/origen/core_ext/string.rb', line 89 def match_all(regex) match_str = self matches = [] while match_str.length > 0 md = match_str.match(regex) break unless md matches << md match_str = md.post_match end matches end |
#pad_leading_zeros(width) ⇒ Object
107 108 109 110 111 |
# File 'lib/origen/core_ext/string.rb', line 107 def pad_leading_zeros(width) str = self (0..(width - size) - 1).each { str = '0' + str } str end |
#squeeze_lines ⇒ Object
162 163 164 |
# File 'lib/origen/core_ext/string.rb', line 162 def squeeze_lines split(/\n+/).join(' ').squeeze(' ') end |
#symbolize ⇒ Object
Sanitizes the string for conversion to a symbol and returns a lower cased symbol version of the string
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/origen/core_ext/string.rb', line 132 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
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/origen/core_ext/string.rb', line 198 def titleize( = {}) = { keep_specials: false }.update() if [:keep_specials] split.map(&:capitalize).join(' ') else split(/ |\_|\-/).map(&:capitalize).join(' ') end end |
#to_bool ⇒ Object
Attempt to convert a String to a boolean answer
167 168 169 170 171 172 173 |
# File 'lib/origen/core_ext/string.rb', line 167 def to_bool if self == true || self =~ (/^(true|t|yes|y|1)$/i) true elsif self == false || empty? || self =~ (/^(false|f|no|n|0)$/i) false end end |
#to_dec ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/origen/core_ext/string.rb', line 13 def to_dec if 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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/origen/core_ext/string.rb', line 113 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_numeric ⇒ Object Also known as: to_number
Convert the String to a Numeric (Float or Integer)
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/origen/core_ext/string.rb', line 184 def to_numeric if numeric? if to_i == to_f # rubocop:disable Lint/FloatComparison to_i else to_f end else fail "'#{self}' cannot be converted to a Numeric, exiting..." end end |
#to_snakecase ⇒ Object Also known as: snakecase
156 157 158 159 |
# File 'lib/origen/core_ext/string.rb', line 156 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
147 148 149 150 151 152 153 |
# File 'lib/origen/core_ext/string.rb', line 147 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 |