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.
Instance Method Summary collapse
-
#split3(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.
Class Method Details
.clean_number(aString) ⇒ Object
58 59 60 |
# File 'lib/buzzcore/string_utils.rb', line 58 def self.clean_number(aString) aString.gsub(/[^0-9.-]/,'') end |
.clean_tag(aTag) ⇒ Object
116 117 118 |
# File 'lib/buzzcore/string_utils.rb', line 116 def self.clean_tag(aTag) aTag.downcase.gsub('_','-').gsub(/[^a-z0-9-]/,'').bite('-').chomp('-') end |
.clean_tags(aTags) ⇒ Object
120 121 122 |
# File 'lib/buzzcore/string_utils.rb', line 120 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/buzzcore/string_utils.rb', line 63 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
37 38 39 40 41 42 |
# File 'lib/buzzcore/string_utils.rb', line 37 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
109 110 111 112 113 114 |
# File 'lib/buzzcore/string_utils.rb', line 109 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
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/buzzcore/string_utils.rb', line 46 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
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/buzzcore/string_utils.rb', line 86 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.
100 101 102 103 104 105 106 |
# File 'lib/buzzcore/string_utils.rb', line 100 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 |
Instance Method Details
#split3(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
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/buzzcore/string_utils.rb', line 23 def split3(aPattern,aOccurence=0) aString = self 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 |