Sha256: 40a7e04f5f546a928f60c45ace26fd2defd36e96b5a810b0b88a96b2ee0349dc

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module StringUtils
	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

	# aTemplate is a string containing tokens like ${SOME_TOKEN}
	# aValues is a hash of token names eg. 'SOME_TOKEN' and their values to substitute
	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
	
	def self.clean_number(aString)
		aString.gsub(/[^0-9.-]/,'')
	end

	# supply a block with 2 parameters, and it will get called for each char as an integer
	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


end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
buzzware-buzzcore-0.2.2 lib/buzzcore/string_utils.rb
buzzware-buzzcore-0.2.3 lib/buzzcore/string_utils.rb
yore-0.0.4 lib/ihl_ruby/string_utils.rb
yore-0.0.3 lib/ihl_ruby/string_utils.rb
yore-0.0.1 lib/ihl_ruby/string_utils.rb
yore-0.0.2 lib/ihl_ruby/string_utils.rb