Class: String
- Defined in:
- lib/kyanite/string/misc.rb,
lib/kyanite/string/cast.rb,
lib/kyanite/string/diff.rb,
lib/kyanite/string/list.rb,
lib/kyanite/string/chars.rb,
lib/kyanite/string/split.rb,
lib/kyanite/string/nested.rb,
lib/kyanite/string/random.rb,
lib/kyanite/string/include.rb,
lib/kyanite/general/classutils.rb,
lib/kyanite/string/chars_const.rb,
lib/kyanite/enumerable/structure.rb
Overview
String Additions
- Kyanite definitions
- Kyanite tests and examples
-
Cast Chars Diff Database-Helper Miscellaneous Nested Split
- Usage
-
require ‘kyanite/string’
Required from Facets String:
- shatter(re)
-
Breaks a string up into an array based on a regular expression. Similar to scan, but includes the matches.
Cast collapse
- HEX_CHARS =
'0123456789abcdef'.freeze
Clear / Format Text collapse
- MYSQL_REPLACES =
[ [/ä/, 'a'], [/ö/, 'o'], [/ü/, 'u'], [/Ä/, 'a'], [/Ö/, 'o'], [/Ü/, 'u'], [/ss/, 'ß'], [/SS/, 'ß'] ]
Constant Summary collapse
- STRING_RANDOM_BASIS =
string_random_basis
Cast collapse
-
#from_x ⇒ String
Get a char for a hex representation.
-
#to_identifier ⇒ Integer
Converts a string into the most plausible Identifier.
-
#to_integer ⇒ Integer
Converts a string to an integer, even if the number was appended to anything.
-
#to_integer_optional ⇒ Integer, String
Tries to convert a string to an integer.
-
#to_nil ⇒ String, Nil
Non-empty strings are returned.
-
#to_x ⇒ String
Get a hex representation for a char.
Overlap / Diff collapse
-
#diff(b) ⇒ String
Returns the differencing part of two strings.
-
#overlap(b) ⇒ String
Returns the mutual part of two strings.
- #overlapdiff(b) ⇒ Array
Database-Helper collapse
-
#enum_to_array ⇒ Array
Converts MySQL-Enum to Array.
-
#list_with(elemente, options = {}, &block) ⇒ String
Generates WHERE clause from Array.
-
#sql_regexp_for_kommaliste ⇒ String
Returns SQL-RegExp for searching in Postgres comma-separated list.
Miscellaneous collapse
-
#count_words ⇒ Integer
Counts the number of words.
-
#include?(input) ⇒ Boolean
Now also accepts an Array as input parameter.
- #is_collection? ⇒ false
-
#mgsub(search_and_replace_pairs) ⇒ String
String substitution like
gsub, but replaces multible patterns in one turn. - #old_include? ⇒ Object
Clear / Format Text collapse
-
#mysqlize ⇒ Object
Converts a string so that you can recognize with utf8_general_ci compared strings.
-
#reduce53(options = {}) ⇒ String
Reduces the string to a base53 encoding.
-
#reduce53!(options = {}) ⇒ String
In-place-variant of reduce53.
-
#reduce94(options = {}) ⇒ String
Reduces the string to a base94 encoding.
-
#reduce94!(options = {}) ⇒ String
In-place-variant of reduce94.
-
#to_a ⇒ Array
reverse of Array#to_s_utf8.
-
#to_array_of_codepoints ⇒ Array
reverse of Array#to_s_utf8.
- #to_array_of_hex ⇒ Array
-
#to_ascii ⇒ Object
Reduces the string to a ASCII encoding.
Upcase and Downcase with support for special letters like german umlauts collapse
-
#capitalize ⇒ String
Converts the first letter to upcase, also works with special letters like german umlauts.
-
#capitalized? ⇒ Boolean
Is the first letter upcase? Also works with special letters like german umlauts.
-
#downcase2 ⇒ String
Better
downcase: also works with special letters like german umlauts. -
#downcase2! ⇒ String
In-place-variant of downcase2.
-
#downcase? ⇒ Boolean
Is the string downcase? Also works with special letters like german umlauts.
-
#upcase2 ⇒ String
Better
upcase: also works with special letters like german umlauts. -
#upcase2! ⇒ String
In-place-variant of upcase2.
-
#upcase? ⇒ Boolean
Is the string upcase? Also works with special letters like german umlauts.
Split collapse
-
#cut(len = 5) ⇒ String
Cuts a string to a maximal length.
-
#extract(start_regexp, stop_regexp) ⇒ String
Extracts a substring using two regular expressions.
-
#fixsize(len) ⇒ String
Forces a fixed size.
-
#nchar(n, replacement = nil) ⇒ String
Returns n characters of the string.
-
#split_by_index(idx) ⇒ Array
Cuts a string in parts with given length.
-
#split_numeric ⇒ Array
Separates a string into numeric and alphanumeric parts.
-
#without_versioninfo ⇒ String
Removes numeric parts and trailing white spaces, hyphens, underscores, and periods.
Nested collapse
-
#anti ⇒ String
Returns the matching opposite bracket.
-
#index_bracket(pattern = nil, start = 0, last_found = nil) ⇒ Range
Returns the positions of the next bracket pair.
-
#mask(options = {}, &block) ⇒ String
Applies the block to a hierarchically defined substring of the string.
-
#nestinglevel(pattern = /[{<(\[]/) ⇒ Integer
Returns the depth of nesting (number of nesting levels).
Random collapse
-
.random(type = :en, size = 16) ⇒ String
Generates a random string.
-
#shuffle(separator = //) ⇒ String
Reorder string in random order.
-
#shuffle!(separator = //) ⇒ String
In-place-variant of
shuffle.
Class Utils collapse
-
#camelize(first_letter_in_uppercase = true) ⇒ String
By default,
camelizeconverts strings to UpperCamelCase. -
#constantize ⇒ Class
Tries to find a constant with the name specified in the argument string:.
-
#demodulize ⇒ String
Removes the module part from the expression in the string.
-
#to_class ⇒ Class
Converts to a class, the reverse of to_classname.
-
#to_classname ⇒ String
Converts to a class name , the reverse of to_class.
-
#underscore ⇒ String
The reverse of
camelize.
Instance Method Summary collapse
Class Method Details
.random(type = :en, size = 16) ⇒ String
Generates a random string. Example:
String.random( :de, 20)
=> brr
121 122 123 |
# File 'lib/kyanite/string/random.rb', line 121 def String.random( type=:en, size=16) (0...size).map { STRING_RANDOM_BASIS[type][Kernel.rand(STRING_RANDOM_BASIS[type].size)] }.join end |
Instance Method Details
#anti ⇒ String
Returns the matching opposite bracket. Examples:
'('.anti -> ')'
'{'.anti -> '}'
']'.anti -> '['
'<hallo>'.anti -> '</hallo>'
'</hallo>'.anti -> '<hallo>'
See tests and examples here.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kyanite/string/nested.rb', line 25 def anti if self.size == 1 return self.tr('([{<)]}>',')]}>([{<') else if self =~ /<([^\/].*)>/ return "</#{$1}>" elsif self =~ /<\/(.*)>/ return "<#{$1}>" end end return self end |
#camelize(first_letter_in_uppercase = true) ⇒ String
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
Examples:
"active_record".camelize # => "ActiveRecord"
"active_record".camelize(:lower) # => "activeRecord"
"active_record/errors".camelize # => "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"
From ActiveSupport, Copyright © 2005 David Heinemeier Hansson. See License.txt.
123 124 125 126 127 128 129 |
# File 'lib/kyanite/general/classutils.rb', line 123 def camelize(first_letter_in_uppercase = true) if first_letter_in_uppercase self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else self.first + camelize(self)[1..-1] end end |
#capitalize ⇒ String
Converts the first letter to upcase, also works with special letters like german umlauts.
221 222 223 |
# File 'lib/kyanite/string/chars.rb', line 221 def capitalize (slice(0) || '').upcase2 + (slice(1..-1) || '').downcase2 end |
#capitalized? ⇒ Boolean
Is the first letter upcase? Also works with special letters like german umlauts.
227 228 229 |
# File 'lib/kyanite/string/chars.rb', line 227 def capitalized? self =~ TR_UPCASE_ALL_REGEXP end |
#constantize ⇒ Class
Tries to find a constant with the name specified in the argument string:
"Module".constantize # => Module
"Test::Unit".constantize # => Test::Unit
The name is assumed to be the one of a top-level constant, no matter whether it starts with “::” or not. No lexical context is taken into account:
C = 'outside'
module M
C = 'inside'
C # => 'inside'
"C".constantize # => 'outside', same as ::C
end
NameError is raised when the name is not in CamelCase or the constant is unknown. From ActiveSupport, Copyright © 2005 David Heinemeier Hansson. See License.txt.
166 167 168 169 170 171 172 |
# File 'lib/kyanite/general/classutils.rb', line 166 def constantize unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self raise NameError, "#{self.inspect} is not a valid constant name!" end Object.module_eval("::#{$1}", __FILE__, __LINE__) end |
#count_words ⇒ Integer
Counts the number of words.
20 21 22 23 24 |
# File 'lib/kyanite/string/misc.rb', line 20 def count_words n = 0 scan(/\b\S+\b/) { n += 1} n end |
#cut(len = 5) ⇒ String
Cuts a string to a maximal length. Example.:
'Hello'.cut(3) => 'Hel'
See tests and examples here.
61 62 63 64 |
# File 'lib/kyanite/string/split.rb', line 61 def cut(len=5) return '' if len <= 0 self[0..len-1] end |
#demodulize ⇒ String
Removes the module part from the expression in the string.
Examples:
"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
"Inflections".demodulize # => "Inflections"
From ActiveSupport, Copyright © 2005 David Heinemeier Hansson. See License.txt.
140 141 142 |
# File 'lib/kyanite/general/classutils.rb', line 140 def demodulize self.gsub(/^.*::/, '') end |
#diff(b) ⇒ String
Returns the differencing part of two strings. Example:
"Hello darling".diff("Hello")
=> " darling"
When in doubt, the longest differencing string. If there is still doubt, then self.
See more examples and tests here.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/kyanite/string/diff.rb', line 41 def diff(b) return self if b.nil? b = b.to_str return '' if self == b # kein Unterschied a = self a,b = b,a if a.size >= b.size # a ist jetzt k?rzer oder gleichlang wie b overlap = a.overlap(b) return self if overlap == '' return b.split(overlap)[1] end |
#downcase2 ⇒ String
Better downcase: also works with special letters like german umlauts. (If you overwrite downcase you will get strange results if you use Active Support.)
See tests and examples here.
193 194 195 |
# File 'lib/kyanite/string/chars.rb', line 193 def downcase2 self.tr(TR_UPCASE, TR_DOWNCASE).downcase end |
#downcase2! ⇒ String
In-place-variant of downcase2.
199 200 201 |
# File 'lib/kyanite/string/chars.rb', line 199 def downcase2! self.tr!(TR_UPCASE, TR_DOWNCASE).downcase! end |
#downcase? ⇒ Boolean
Is the string downcase? Also works with special letters like german umlauts.
237 238 239 |
# File 'lib/kyanite/string/chars.rb', line 237 def downcase? (self == self.upcase) end |
#enum_to_array ⇒ Array
Converts MySQL-Enum to Array.
84 85 86 |
# File 'lib/kyanite/string/list.rb', line 84 def enum_to_array self[5..-2].gsub("'",'').split(',').collect {|i| [i,i] } end |
#extract(start_regexp, stop_regexp) ⇒ String
Extracts a substring using two regular expressions. Example:
string = '<select id="hello"><option value="0">none</option></select>'
string.extract( /select.*?id="/ , '"' ) => 'hello'
See tests and examples here.
105 106 107 |
# File 'lib/kyanite/string/split.rb', line 105 def extract( start_regexp, stop_regexp ) split(start_regexp)[1].split(stop_regexp)[0] end |
#fixsize(len) ⇒ String
Forces a fixed size.
See tests and examples here.
71 72 73 74 75 76 77 78 |
# File 'lib/kyanite/string/split.rb', line 71 def fixsize( len ) return '' if len <= 0 if self.size < len self.ljust(len) else self[0..len-1] end end |
#from_x ⇒ String
Get a char for a hex representation.
See also to_x.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/kyanite/string/cast.rb', line 91 def from_x str, q, first = '', 0, false each_byte { |byte| # Our hex chars are 2 bytes wide, so we have to keep track # of whether it's the first or the second of the two. # # NOTE: inject with each_slice(2) would be a natural fit, # but it's kind of slow... if first = !first q = HEX_CHARS.index(byte) else # Now we got both parts, so let's do the # inverse of divmod(16): q * 16 + r str << q * 16 + HEX_CHARS.index(byte) end } str end |
#include?(input) ⇒ Boolean
Now also accepts an Array as input parameter. The array elements are ORed, i.e. include? is true if old_include? is true for at least one element of the array. All strings include ”, [] or nil. Nil does not include anything: nil.include? => false
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kyanite/string/include.rb', line 20 def include?(input) return true if input.nil? return true if input.empty? if ( input.respond_to?(:each) && !input.kind_of?(String) ) input.each do |frag| return true if include?(frag) end false else old_include?(input) end end |
#index_bracket(pattern = nil, start = 0, last_found = nil) ⇒ Range
Returns the positions of the next bracket pair. Example:
'Hello(welt)wort'.index_bracket -> 5..10
See tests and examples here.
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 |
# File 'lib/kyanite/string/nested.rb', line 47 def index_bracket( pattern=nil, start=0, last_found = nil ) return nil if self.empty? pattern = /[{<\[]/ unless pattern # pattern = /['"({<(\[]/ unless pattern debug = false puts 'untersuche ' + self[start..-1] if debug found = self.index(pattern, start) puts "found=#{found}" if debug return last_found unless found pattern_anti = self[found..found].anti startpunkt = found loop do found_next = self.index( pattern, startpunkt+1 ) || 9999999 found_anti = self.index( pattern_anti, startpunkt+1 ) puts "found_next=#{found_next}" if debug puts "found_anti=#{found_anti}" if debug break unless found_anti return found..found_anti if found_anti <= found_next # puts # puts # puts # puts # puts # puts "start=#{(start).inspect_pp}" # puts "pattern=#{(pattern).inspect_pp}" # puts "found_next=#{(found_next).inspect_pp}" # puts "found..found_anti=#{(found..found_anti).inspect_pp}" rekursiv_result = self.index_bracket(pattern, found_next, found..found_anti) return found..found_anti unless rekursiv_result startpunkt = rekursiv_result.last puts "startpunkt=#{startpunkt}" if debug end # loop nil end |
#is_collection? ⇒ false
48 |
# File 'lib/kyanite/enumerable/structure.rb', line 48 def is_collection?; false; end |
#list_with(elemente, options = {}, &block) ⇒ String
Generates WHERE clause from Array.
Example:
array = ['Anna','Birte','Charlie']
"kisses_from = ".list_with(array)
=> "kisses_from = 'Anna' OR kisses_from = 'Birte' OR kisses_from = 'Charlie'"
See tests and more examples here.
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 |
# File 'lib/kyanite/string/list.rb', line 26 def list_with( elemente, = {}, &block ) = { :pre => %q{'}, :post => %q{'}, :sep => ' OR ', :empty => 'FALSE'}.merge() # keine Liste angegeben return [:empty] if elemente.empty? # einzelnen String oder einzelnes Symbol angegeben -> ohne Separator ausgeben if elemente.kind_of?(String) || !elemente.respond_to?(:each_index) e = elemente.dup e = yield e if block_given? return "#{self}#{options[:pre]}#{e}#{options[:post]}" end # Liste hat nur ein Element -> ohne Separator ausgeben if elemente.size <= 1 e = elemente[0].dup e = yield e if block_given? return "#{self}#{options[:pre]}#{e}#{options[:post]}" end # Liste hat mehrere Elemente result = '' elemente[0..-2].each do |e| # Die vorderen Elemente mit Separator e = yield e if block_given? result += "#{self}#{options[:pre]}#{e}#{options[:post]}#{options[:sep]}" end # Letztes Element ohne Separator e = elemente[-1].dup e = yield elemente[-1] if block_given? result += "#{self}#{options[:pre]}#{e}#{options[:post]}" result end |
#mask(options = {}, &block) ⇒ String
Applies the block to a hierarchically defined substring of the string.
See tests and examples here.
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/kyanite/string/nested.rb', line 97 def mask( ={}, &block ) # vorbereiten debug = false result = self.dup level_start = [:level_start] || 1 level_end = [:level_end] || 99999 level_akt = [:level_akt] || 0 #level_akt += 1 if with_brackets pattern = [:pattern] || /[{<\[]/ # /['"({<\[]/ ist langsam skip_empty = [:skip_empty] || false param_level = [:param_level] || false # übergibt dem Block zusätzlich die Nummer des aktuellen Levels with_brackets = [:with_brackets] || false # übergibt dem Block auch die Brackets, Beispiel siehe Tests!! raise ArgumentError, "level_start can't be nil" unless level_start raise ArgumentError, "level_end can't be nil" unless level_end raise ArgumentError, 'level_end has to be >= level_start' unless level_end >= level_start if debug puts "level_start=#{level_start}" puts "level_end=#{level_end}" puts "level_akt=#{level_akt}" puts end geklammert = result.index_bracket(pattern) puts "geklammert=#{geklammert}" if debug # Los geht's: geklammert, Klammern werden nicht mit übergeben if geklammert if !with_brackets if geklammert.first > 0 pre = result[0..geklammert.first-1] else pre = '' end bra = result[geklammert.first..geklammert.first] mid = result[geklammert.first+1..geklammert.last-1] ket = result[geklammert.last..geklammert.last] if geklammert.last < (result.size-1) past = result[geklammert.last+1..-1] else past = '' end else # with_brackets if geklammert.first > 0 pre = result[0..geklammert.first] else pre = result[geklammert.first..geklammert.first] end bra = '' mid = result[geklammert.first+1..geklammert.last-1] ket = '' if geklammert.last < (result.size-1) past = result[geklammert.last..-1] else past = result[geklammert.last..geklammert.last] end end if debug puts "1pre=#{pre}" puts "1bra=#{bra}" puts "1mid=#{mid}" puts "1ket=#{ket}" puts "1past=#{past}" puts end # yield if ( (level_start..level_end) === level_akt && (!pre.empty? || !skip_empty) ) if param_level pre = yield(pre,level_akt) else pre = yield(pre) end end # if yield mid = mid.mask( .merge({:level_akt => level_akt+1}), &block ) past = past.mask( , &block ) if debug puts "2pre=#{pre}" puts "2bra=#{bra}" puts "2mid=#{mid}" puts "2ket=#{ket}" puts "2past=#{past}" puts end return (pre||'') + bra + (mid||'') + ket + (past||'') # Los geht's: keine Klammern else # yield if ( (level_start..level_end) === level_akt && (!result.empty? || !skip_empty ) ) puts "result=#{result}\n" if debug if param_level result = yield(result,level_akt) else result= yield(result) end end # if yield return (result||'') end raise 'no go' end |
#mgsub(search_and_replace_pairs) ⇒ String
String substitution like gsub, but replaces multible patterns in one turn. Example:
"between".mgsub([[/ee/, 'II'], [/e/, 'E']])
=> "bEtwIIn"
Tests here.
32 33 34 35 36 37 |
# File 'lib/kyanite/string/misc.rb', line 32 def mgsub(search_and_replace_pairs) patterns = search_and_replace_pairs.collect { |search, replace| search } gsub(Regexp.union(*patterns)) do |match| search_and_replace_pairs.detect{ |search, replace| search =~ match}[1] end end |
#mysqlize ⇒ Object
Converts a string so that you can recognize with utf8_general_ci compared strings
165 166 167 |
# File 'lib/kyanite/string/chars.rb', line 165 def mysqlize self.mgsub(MYSQL_REPLACES).downcase.to_s end |
#nchar(n, replacement = nil) ⇒ String
Returns n characters of the string. If n is positive the characters are from the beginning of the string. If n is negative from the end of the string.
Alternatively a replacement string can be given, which will replace the n characters. Example:
'abcde'.nchar(1) => 'a'
'abcde'.nchar(2) => 'ab'
'abcde'.nchar(3) => 'abc'
'abcde'.nchar(2,'') => 'cde'
(The originaly version of this method is from the Facets library). See tests and examples here.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kyanite/string/split.rb', line 39 def nchar(n, replacement=nil) if replacement return self if n == 0 return '' if (n.abs >= self.length) s = self.dup n > 0 ? (s[0...n] = replacement) : (s[n..-1] = replacement) return s # ohne replacement else return '' if n == 0 return self if (n.abs > self.length) n > 0 ? self[0...n] : self[n..-1] end end |
#nestinglevel(pattern = /[{<(\[]/) ⇒ Integer
Returns the depth of nesting (number of nesting levels).
213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/kyanite/string/nested.rb', line 213 def nestinglevel(pattern=/[{<(\[]/) result = 0 self.mask( :level_start => 0, :pattern => pattern, :param_level => true ) { |s,l| if l > result result = l s else s end } result end |
#old_include? ⇒ Object
13 |
# File 'lib/kyanite/string/include.rb', line 13 alias :old_include? :include? |
#overlap(b) ⇒ String
Returns the mutual part of two strings. Example:
"Hello world".overlap("Hello darling")
=> "Hello"
See more examples and tests here.
21 22 23 24 25 26 27 28 29 |
# File 'lib/kyanite/string/diff.rb', line 21 def overlap(b) return '' if b.nil? b = b.to_str return self if self == b return '' if self[0] != b[0] n = [self.size, b.size].min (0..n).each { |i| return self[0..i-1] unless self[i] == b[i] } end |
#overlapdiff(b) ⇒ Array
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kyanite/string/diff.rb', line 66 def overlapdiff(b) return '', self if b.nil? b = b.to_str return self,'' if self == b # kein Unterschied a = self a,b = b,a if a.size >= b.size # a ist jetzt k?rzer oder gleichlang wie b overlap = a.overlap(b) return overlap, self if overlap == '' return overlap, b.split(overlap)[1] end |
#reduce53(options = {}) ⇒ String
Reduces the string to a base53 encoding. The result consists only uppercase letters, minus, and lowercase characters as replacement for some known special characters.
-
Removes all non-letter-chars.
-
Converts all regular letters to upcase letters.
-
Converts special letters to reduced downcase letters, eg. àáâăäãāåạąæảấầắằÀÁÂĂÄÃĀÅẠĄÆẢẤẦẮẰ etc. to aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.
-
Caution: Also Newlines are removed.
See tests and examples here.
116 117 118 |
# File 'lib/kyanite/string/chars.rb', line 116 def reduce53( ={} ) dup.reduce53!() end |
#reduce53!(options = {}) ⇒ String
In-place-variant of reduce53.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/kyanite/string/chars.rb', line 123 def reduce53!( ={} ) if [:camelcase] self.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1-\2') self.gsub!(/([a-z\d])([A-Z])/,'\1-\2') end self.gsub!( 'ß', [:german_sz] ) if [:german_sz] self.tr!('abcdefghijklmnopqrstuvwxyz§', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ') self.tr!(TR_FULL, TR_REDUCED.downcase) unless [:space] self.delete!('^- A-Za-z') else self.tr!('^- A-Za-z', ' ') end self.gsub!(/-+/, ' ') self.gsub!(/\s+/, ' ') self.strip! self.gsub!(/ /, '-') self end |
#reduce94(options = {}) ⇒ String
Reduces the string to a base94 encoding. About 10 times faster than with UnicodeUtils.
-
Converts àáâăäãāåạąæảấầắằÀÁÂĂÄÃĀÅẠĄÆẢẤẦẮẰ etc. to aaaaaaaaaaaaaaaaAAAAAAAAAAAAAAAA.
-
Then removes all non-Ascii-chars.
-
Then removes all non-printable Ascii-chars.
-
Caution: Also Newlines are removed.
See tests and examples here.
85 86 87 |
# File 'lib/kyanite/string/chars.rb', line 85 def reduce94( ={} ) dup.reduce94!() end |
#reduce94!(options = {}) ⇒ String
In-place-variant of reduce94.
92 93 94 95 96 97 |
# File 'lib/kyanite/string/chars.rb', line 92 def reduce94!( ={} ) self.gsub!( 'ß', [:german_sz] ) if [:german_sz] self.tr!(TR_FULL, TR_REDUCED) self.delete!('^ -~') self end |
#shuffle(separator = //) ⇒ String
Reorder string in random order. Example:
"Random order".shuffle
=> "oeo rdRdnmar"
97 98 99 |
# File 'lib/kyanite/string/random.rb', line 97 def shuffle(separator=//) split(separator).shuffle.join('') end |
#shuffle!(separator = //) ⇒ String
In-place-variant of shuffle.
103 104 105 |
# File 'lib/kyanite/string/random.rb', line 103 def shuffle!(separator=//) self.replace( shuffle(separator) ) end |
#split_by_index(idx) ⇒ Array
Cuts a string in parts with given length.
See tests and examples here.
85 86 87 88 89 90 91 92 93 |
# File 'lib/kyanite/string/split.rb', line 85 def split_by_index(idx) if idx.kind_of?(Integer) [nchar(idx)] + [nchar(idx,'')] elsif idx.kind_of?(Array) [nchar(idx[0])] + nchar(idx[0],'').split_by_index(idx.shift_complement) end # if end |
#split_numeric ⇒ Array
Separates a string into numeric and alphanumeric parts. Currently works only with positive integers. Example:
'abc123'.split_numeric >> ['abc',123] (Array)
'123abc'.split_numeric >> [123,'abc'] (Array)
'123'.split_numeric >> 123 (Integer)
'abc'.split_numeric >> 'abc' (String)
It even works with more than two parts:
'123abc456'.split_numeric >> [123,'abc',456]
'abc123def'.split_numeric >> ['abc',123,'def']
See tests and examples here.
126 127 128 129 130 |
# File 'lib/kyanite/string/split.rb', line 126 def split_numeric result = shatter(/\d+/).collect{ |i| i.to_integer_optional } return result[0] if ( result.is_collection? && result.size == 1 ) return result end |
#sql_regexp_for_kommaliste ⇒ String
Returns SQL-RegExp for searching in Postgres comma-separated list.
73 74 75 76 77 78 |
# File 'lib/kyanite/string/list.rb', line 73 def sql_regexp_for_kommaliste '[, ]' + self + '[, ]' + '|' + # match mittendrin '^' + self + '[, ]' + '|' + # match am Anfang '[, ]' + self + '$' + '|' + # match am Ende '^' + self + '$' # match von Anfang bis Ende end |
#to_a ⇒ Array
reverse of Array#to_s_utf8
27 28 29 30 31 32 33 |
# File 'lib/kyanite/string/chars.rb', line 27 def to_a result = [] self.each_char do |c| result << c end result end |
#to_array_of_codepoints ⇒ Array
reverse of Array#to_s_utf8
38 39 40 |
# File 'lib/kyanite/string/chars.rb', line 38 def to_array_of_codepoints self.codepoints.to_a end |
#to_array_of_hex ⇒ Array
43 44 45 |
# File 'lib/kyanite/string/chars.rb', line 43 def to_array_of_hex self.unpack('U'*self.length).collect {|x| x.to_s 16} end |
#to_ascii ⇒ Object
Reduces the string to a ASCII encoding. Example:
ffi = "\uFB03"
ix = "\u2168"
high23="²³"
high5 = "\u2075"
all = ffi + ix + high23 + high5
all.to_ascii
=> "ffiIX235"
Based on UnicodeUtils.nfkd, but handles all characters from ISO/IEC 8859-1 and CP1252 like humans do, not just deleting the accents. Example:
"ÄÖÜäöüß".to_ascii
=> "AeOeUeaeoeuess"
-
Converts ÄÖÜäöüßàáâăäãāåạąæảấầắằ etc. to AeOeUeaeoeuessaaaaaaaaaaaaaaaa.
-
Then removes all non-Ascii-chars.
-
Then removes all non-printable Ascii-chars.
-
Caution: Also Newlines are removed.
About 10 times slower than reduce94, but more accurate.
68 69 70 71 72 |
# File 'lib/kyanite/string/chars.rb', line 68 def to_ascii result = self.to_ascii_extra_chars result.tr!(TR_FULL, TR_REDUCED) # not necessary, only for performance return UnicodeUtils.nfkd(result).delete('^ -~') # delete is faster than gsub end |
#to_ascii_extra_chars ⇒ Object
240 241 242 243 244 245 |
# File 'lib/kyanite/string/chars_const.rb', line 240 def to_ascii_extra_chars result = tr(TR_FULL_TO_ASCII, TR_REDUCED_TO_ASCII) result.gsub(RE_EXTRA_CHARS) do |match| TR_EXTRA_CHARS.detect{ |search, replace| search =~ match}[1] end end |
#to_ascii_minus ⇒ Object
248 249 250 |
# File 'lib/kyanite/string/chars_const.rb', line 248 def to_ascii_minus end |
#to_class ⇒ Class
Converts to a class, the reverse of to_classname
Defined for classes Class, Symbol, String. Accepts both CamelCase and down_case.
Tests and examples here.
82 83 84 85 86 |
# File 'lib/kyanite/general/classutils.rb', line 82 def to_class self.camelize.constantize rescue return nil end |
#to_classname ⇒ String
70 71 72 |
# File 'lib/kyanite/general/classutils.rb', line 70 def to_classname self.demodulize.underscore end |
#to_identifier ⇒ Integer
Converts a string into the most plausible Identifier
See examples and tests here.
22 23 24 |
# File 'lib/kyanite/string/cast.rb', line 22 def to_identifier self.strip.to_integer_optional end |
#to_integer ⇒ Integer
Converts a string to an integer, even if the number was appended to anything. Unlike to_i it returns nil if no integer was found inside the string.
See examples and tests here.
33 34 35 36 37 38 |
# File 'lib/kyanite/string/cast.rb', line 33 def to_integer return nil unless self =~ /\d/ firsttry = self.to_i return firsttry if firsttry != 0 return self.scan(/\d+/)[0].to_i end |
#to_integer_optional ⇒ Integer, String
Tries to convert a string to an integer. Returns self if the string does not start with a number. Empty strings are converted to nil.
See examples and tests here.
48 49 50 51 52 |
# File 'lib/kyanite/string/cast.rb', line 48 def to_integer_optional return nil if self.empty? return self unless (self =~ /^\d/ || self =~ /^-\d/ ) return self.to_i end |
#to_nil ⇒ String, Nil
Non-empty strings are returned. Empty strings are converted to nil.
58 59 60 61 |
# File 'lib/kyanite/string/cast.rb', line 58 def to_nil return self unless self.empty? nil end |
#to_x ⇒ String
Get a hex representation for a char. See also from_x.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kyanite/string/cast.rb', line 75 def to_x hex = '' each_byte { |byte| # To get a hex representation for a char we just utilize # the quotient and the remainder of division by base 16. q, r = byte.divmod(16) hex << HEX_CHARS[q] << HEX_CHARS[r] } hex end |
#underscore ⇒ String
The reverse of camelize. Makes an underscored, lowercase form from the expression in the string. Changes ‘::’ to ‘/’ to convert namespaces to paths.
Examples:
"ActiveRecord".underscore # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors
From ActiveSupport, Copyright © 2005 David Heinemeier Hansson. See License.txt.
100 101 102 103 104 105 106 |
# File 'lib/kyanite/general/classutils.rb', line 100 def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
#upcase2 ⇒ String
Better upcase: also works with special letters like german umlauts. (If you overwrite upcase you will get strange results if you use Active Support.)
See tests and examples here.
209 210 211 |
# File 'lib/kyanite/string/chars.rb', line 209 def upcase2 self.tr(TR_DOWNCASE, TR_UPCASE).upcase end |
#upcase2! ⇒ String
In-place-variant of upcase2.
215 216 217 |
# File 'lib/kyanite/string/chars.rb', line 215 def upcase2! self.tr!(TR_DOWNCASE, TR_UPCASE).upcase! end |
#upcase? ⇒ Boolean
Is the string upcase? Also works with special letters like german umlauts.
232 233 234 |
# File 'lib/kyanite/string/chars.rb', line 232 def upcase? (self == self.upcase) end |
#without_versioninfo ⇒ String
Removes numeric parts and trailing white spaces, hyphens, underscores, and periods.
See tests and examples here.
137 138 139 |
# File 'lib/kyanite/string/split.rb', line 137 def without_versioninfo shatter(/\d+/)[0].strip.chomp('_').chomp('-').chomp('.') end |