Module: StringUtils
- Defined in:
- lib/buzzcore/string_utils.rb
Class Method Summary collapse
- .clean_number(aString) ⇒ Object
- .clean_tag(aTag) ⇒ Object
- .clean_tags(aTags) ⇒ Object
- .crop(aString, aLength, aEllipsis = true, aConvertNil = true) ⇒ Object
- .crop_to_word_count(aText, aWordCount) ⇒ Object
-
.each_unicode_char(aString) ⇒ Object
supply a block with 2 parameters, and it will get called for each char as an integer.
- .random_word(min, max) ⇒ Object
-
.reduce_whitespace(aText) ⇒ Object
replaces all tabs with spaces, and reduces multiple spaces to a single space.
-
.render_template(aTemplate, aValues) ⇒ Object
aTemplate is a string containing tokens like $SOME_TOKEN aValues is a hash of token names eg.
- .simplify_whitespace(aText) ⇒ Object
-
.split3(aString, aPattern, aOccurence = 0) ⇒ Object
given (‘abcdefg’,‘c.*?e’) returns [‘ab’,‘cde’,‘fg’] so you can manipulate the head, match and tail seperately, and potentially rejoin.
-
.word_safe_truncate(aString, aMaxLength) ⇒ Object
truncates a string to the given length by looking for the previous space.
Class Method Details
.clean_number(aString) ⇒ Object
43 44 45 |
# File 'lib/buzzcore/string_utils.rb', line 43 def self.clean_number(aString) aString.gsub(/[^0-9.-]/,'') end |
.clean_tag(aTag) ⇒ Object
101 102 103 |
# File 'lib/buzzcore/string_utils.rb', line 101 def self.clean_tag(aTag) aTag.downcase.gsub('_','-').gsub(/[^a-z0-9-]/,'').bite('-').chomp('-') end |
.clean_tags(aTags) ⇒ Object
105 106 107 |
# File 'lib/buzzcore/string_utils.rb', line 105 def self.(aTags) aTags.map {|t| clean_tag(t)}.uniq end |
.crop(aString, aLength, aEllipsis = true, aConvertNil = true) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/buzzcore/string_utils.rb', line 2 def self.crop(aString,aLength,aEllipsis=true,aConvertNil=true) return aConvertNil ? ' '*aLength : nil if !aString increase = aLength-aString.length return aString+' '*increase if increase>=0 return aEllipsis ? aString[0,aLength-3]+'...' : aString[0,aLength] end |
.crop_to_word_count(aText, aWordCount) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/buzzcore/string_utils.rb', line 14 def self.crop_to_word_count(aText,aWordCount) aText = simplify_whitespace(aText) matches = aText.scan_md(/[^\w-]+/) match = matches[aWordCount-1] result = (match ? match.pre_match : aText) result end |
.each_unicode_char(aString) ⇒ Object
supply a block with 2 parameters, and it will get called for each char as an integer
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/buzzcore/string_utils.rb', line 48 def self.each_unicode_char(aString) len = 1 index = 0 char = 0 aString.each_byte do |b| if index==0 len = 1 len = 2 if b & 0b11000000 != 0 len = 3 if b & 0b11100000 != 0 len = 4 if b & 0b11110000 != 0 char = 0 end char |= b << index*8 yield(char,len) if index==len-1 # last byte; char is complete index += 1 index = 0 if index >= len end end |
.random_word(min, max) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/buzzcore/string_utils.rb', line 22 def self.random_word(min,max) len = min + rand(max-min+1) result = ' '*len (len-1).downto(0) {|i| result[i] = (?a + rand(?z-?a+1)).chr} return result end |
.reduce_whitespace(aText) ⇒ Object
replaces all tabs with spaces, and reduces multiple spaces to a single space
94 95 96 97 98 99 |
# File 'lib/buzzcore/string_utils.rb', line 94 def self.reduce_whitespace(aText) aText = aText.gsub("\t"," ") # replace tabs with spaces aText.strip! aText.squeeze!(' ') aText end |
.render_template(aTemplate, aValues) ⇒ Object
aTemplate is a string containing tokens like $SOME_TOKEN aValues is a hash of token names eg. ‘SOME_TOKEN’ and their values to substitute
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/buzzcore/string_utils.rb', line 31 def self.render_template(aTemplate,aValues) # get positions of tokens result = aTemplate.gsub(/\$\{(.*?)\}/) do |s| key = s[2..-2] rep = (aValues[key] || s) #puts "replacing #{s} with #{rep}" rep end #puts "rendered :\n#{result}" return result end |
.simplify_whitespace(aText) ⇒ Object
10 11 12 |
# File 'lib/buzzcore/string_utils.rb', line 10 def self.simplify_whitespace(aText) aText.gsub(/[ \n\t\r]+/,' ').strip end |
.split3(aString, aPattern, aOccurence = 0) ⇒ Object
given (‘abcdefg’,‘c.*?e’) returns [‘ab’,‘cde’,‘fg’] so you can manipulate the head, match and tail seperately, and potentially rejoin
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/buzzcore/string_utils.rb', line 71 def self.split3(aString,aPattern,aOccurence=0) matches = aString.scan_md(aPattern) match = matches[aOccurence] parts = (match ? [match.pre_match,match.to_s,match.post_match] : [aString,nil,'']) if !block_given? # return head,match,tail parts else # return string parts[1] = yield *parts if match parts.join end end |
.word_safe_truncate(aString, aMaxLength) ⇒ Object
truncates a string to the given length by looking for the previous space.
85 86 87 88 89 90 91 |
# File 'lib/buzzcore/string_utils.rb', line 85 def self.word_safe_truncate(aString,aMaxLength) return nil if !aString return aString if aString.length <= aMaxLength posLastSpace = aString.rindex(/[ \t]/,aMaxLength) return aString[0,aMaxLength] if !posLastSpace aString[0,posLastSpace] end |