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
103 104 105 106 |
# File 'lib/origen/core_ext/string.rb', line 103 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
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 87 |
# File 'lib/origen/core_ext/string.rb', line 24 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
234 235 236 237 238 |
# File 'lib/origen/core_ext/string.rb', line 234 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
228 229 230 |
# File 'lib/origen/core_ext/string.rb', line 228 def is_downcase? self == downcase end |
#is_numeric? ⇒ Boolean Also known as: numeric?
Check if a String is a numeric
177 178 179 180 181 |
# File 'lib/origen/core_ext/string.rb', line 177 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
221 222 223 |
# File 'lib/origen/core_ext/string.rb', line 221 def is_upcase? self == upcase end |
#is_verilog_number? ⇒ Boolean
210 211 212 213 214 215 216 217 |
# File 'lib/origen/core_ext/string.rb', line 210 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
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/origen/core_ext/string.rb', line 90 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
108 109 110 111 112 |
# File 'lib/origen/core_ext/string.rb', line 108 def pad_leading_zeros(width) str = self (0..(width - size) - 1).each { str = '0' + str } str end |
#squeeze_lines ⇒ Object
163 164 165 |
# File 'lib/origen/core_ext/string.rb', line 163 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
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/origen/core_ext/string.rb', line 133 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
199 200 201 202 203 204 205 206 207 208 |
# File 'lib/origen/core_ext/string.rb', line 199 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
168 169 170 171 172 173 174 |
# File 'lib/origen/core_ext/string.rb', line 168 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
14 15 16 17 18 19 20 21 22 |
# File 'lib/origen/core_ext/string.rb', line 14 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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/origen/core_ext/string.rb', line 114 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)
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/origen/core_ext/string.rb', line 185 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
157 158 159 160 |
# File 'lib/origen/core_ext/string.rb', line 157 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
148 149 150 151 152 153 154 |
# File 'lib/origen/core_ext/string.rb', line 148 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 |