Module: StringUtils

Defined in:
lib/buzzcore/string_utils.rb

Class Method Summary collapse

Class Method Details

.clean_number(aString) ⇒ Object



24
25
26
# File 'lib/buzzcore/string_utils.rb', line 24

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

.each_unicode_char(aString) ⇒ Object

supply a block with 2 parameters, and it will get called for each char as an integer



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/buzzcore/string_utils.rb', line 29

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

.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



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/buzzcore/string_utils.rb', line 12

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