Module: StringUtils

Defined in:
lib/buzzcorej/string_utils.rb

Class Method Summary collapse

Class Method Details

.clean_number(aString) ⇒ Object



36
37
38
# File 'lib/buzzcorej/string_utils.rb', line 36

def self.clean_number(aString)
	aString.gsub(/[^0-9.-]/,'')
end

.crop(aString, aLength, aEllipsis = true, aConvertNil = true) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/buzzcorej/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/buzzcorej/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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/buzzcorej/string_utils.rb', line 41

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

.reduce_whitespace(aText) ⇒ Object

replaces all tabs with spaces, and reduces multiple spaces to a single space



87
88
89
90
91
92
# File 'lib/buzzcorej/string_utils.rb', line 87

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



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/buzzcorej/string_utils.rb', line 24

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/buzzcorej/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



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/buzzcorej/string_utils.rb', line 64

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.



78
79
80
81
82
83
84
# File 'lib/buzzcorej/string_utils.rb', line 78

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