Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/gstring.rb
Overview
FEATURES TODO NEXT REVISION ## 1: to_morse (maybe … limited appeal my guess) 2: encryption? (maybe a separate gem, or include a wrapper from that gem) 3: format number to engineering notation … 15530.to_eng => 1515.53e3
Constant Summary collapse
- SI_UNIT_PREFIXES =
{1=>'da', 2=>'h', 3=>'k', 6=>'M', 9=>'G', 12=>'T', 15=>'P', 18=>'E', 21=>'Z', 24=>'Y', 27=>'kY', 30=>'MY', 33=>'GY', 36=>'TY', 39=>'PY', 42=>'EY', 45=>'ZY', 48=>'YY', -1=>'d',-2=>'c', -3=>'m', -6=>'µ',-9=>'n',-12=>'p',-15=>'f',-18=>'a',-21=>'z',-24=>'y',-27=>'my',-30=>'µy', -33=>'ny', -36=>'py', -39=>'fy', -42=>'ay', -45=>'zy', -48=>'yy'}
- RGX_FLOAT =
/\A[\+\-]?(0|[1-9]\d*)(([eE][\+\-]?\d+)|(\.\d+((e)?[\+\-]?\d+)?))/- STD_ESCAPE_SET_RUBY =
[0..31,'"',"'","\\","\;","\#"].to_bset
- STD_ESCAPE_HASH =
{7=>"\\a", 8=>"\\b", 12=>"\\f", 10=>"\\n", 13=>"\\r", 9=>"\\t", 11=>"\\v"}
- SET_PARSE_CHARS =
BitSet.new.add_parse_chars!
- SET_SPLIT_CHARS =
SET_PARSE_CHARS | "_#`\""
- SET_UPPERS =
BitSet.uppercase_chars
- SET_LOWERS =
BitSet.lowercase_chars
- SET_CHARS =
BitSet.letter_chars
- SET_INT_CHARS =
BitSet.digit_chars
- GS_SENTENCE_TERM =
'?!.'.to_bset
- GS_TITLE_EXCEPTIONS =
{ "a" =>true, "an" =>true, "and" =>true, "as" =>true, "at" =>true, "but" =>true, "by" =>true, "for" =>true, "from" =>true, "in" =>true, "nor" =>true, "of" =>true, "on" =>true, "or" =>true, "the" =>true, "to" =>true, "up" =>true }
- SET_SUP_CHARS =
(SET_CHARS | SET_INT_CHARS | "+-=()βγδεθινΦφχ") - "qCFQSXYZC"
- SET_SUB_CHARS =
"aeijoruvx" | SET_INT_CHARS | "βγρφχ()+-="
- SET_LIKE_KEEP =
SET_CHARS | SET_INT_CHARS | "*-'"
- R_RATED =
/(cum[\-_s5]|pi[s5][s5]|f.ck|4k.r|whore|slut|sh[iy]t|n[i1l][g9][g9]er|qqqq|my.*a[sz]|ph.ck|dick|vagina|a[sz5][sz5].*wipe|a[sz5][sz5].*hole|penis|b4i4q)/i- ROT_STRING =
ROT_STRING = ‘ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?/>.<,“':;|\}]{[+=_-)(*&^%$#@!`~Ω≈ç√∫˜µ≤≥÷åß∂ƒ©˙∆˚¬…æœ∑´®†¥¨ˆøπ“‘«`¡™£¢∞§¶•ªº–≠¸˛Ç◊ı˜Â¯˘¿ÅÍÎÏ˝ÓÔÒÚÆŒ„´‰ˇÁ¨ˆØ∏”’»`⁄€‹›fifl‡°·‚—±’
'{Copyright© 2016_BRYAN-cOLVIn=al|.®¡GHTS/4ME},+bd¯˘¿ÅÍe^%$f∛jΆ¥w¼∜x7Ï˝ÓÔk”’»⁄€‹×›fifl‡°·‚KPøπ“‘s½u!~Ω≈çzD∮5vF∂«`™£WJŒ„‰ˇÁ¨ˆØ∏3QℵU(*&#@√∫µ≤≥÷åßXZmqƒ˙∆˚¬…æœ∑´89?><"\':;\\][)¢∞§¶•ªº–≠¸˛Ç◊ı˜ÒÚÆ—±'- ROT_LEN =
ROT_STRING.length
- SET_COMPACT_RIGHT =
"<[{(".to_bset
- SET_COMPACT_LEFT =
".,!?:;>]})'\"\\/|-_+".to_bset
- SET_VERTICLE =
"\n\v".to_bset
- @@gs_bracketing_pairs =
{ '[' => ']', '(' => ')', '{' => '}', '<' => '>' }
- @@gs_sup_hash =
{'β'=>'ᵝ', 'γ'=>'ᵞ', 'δ'=>'ᵟ', 'ε'=>'ᵋ', 'θ'=>'ᶿ', 'ι'=>'ᶥ', 'ν'=>'ᶹ', 'Φ'=>'ᶲ', 'φ'=>'ᵠ', 'χ'=>'ᵡ', '1'=>'¹','2'=>'²','3'=>'³','0'=>'⁰','-'=>'⁻','+'=>'⁺', '='=>'⁼', '('=>'⁽', ')'=>'⁾', 'A'=>'ᴬ','B'=>'ᴮ','D'=>'ᴰ','E'=>'ᴱ','G'=>'ᴳ','H'=>'ᴴ','I'=>'ᴵ','J'=>'ᴶ','K'=>'ᴷ', 'L'=>'ᴸ','M'=>'ᴹ','N'=>'ᴺ','O'=>'ᴼ','P'=>'ᴾ','R'=>'ᴿ','T'=>'ᵀ','U'=>'ᵁ','V'=>'ⱽ','W'=>'ᵂ', 'a'=>'ᵃ','b'=>'ᵇ','c'=>'ᶜ','d'=>'ᵈ','e'=>'ᵉ','f'=>'ᶠ','g'=>'ᵍ','h'=>'ʰ', 'i'=>'ⁱ','j'=>'ʲ','k'=>'ᵏ','l'=>'ˡ','m'=>'ᵐ','n'=>'ⁿ','o'=>'ᵒ','p'=>'ᵖ','r'=>'ʳ', 's'=>'ˢ','t'=>'ᵗ','u'=>'ᵘ','v'=>'ᵛ','w'=>'ʷ','x'=>'ˣ','y'=>'ʸ','z'=>'ᶻ'}
- @@gs_sub_hash =
{'a'=>'ₐ', 'e'=>'ₑ','i'=>'ᵢ','j'=>'ⱼ','o'=>'ₒ','r'=>'ᵣ','u'=>'ᵤ','v'=>'ᵥ', 'x'=>'ₓ','β'=>'ᵦ','γ'=>'ᵧ','ρ'=>'ᵨ','φ'=>'ᵩ','χ'=>'ᵪ','='=>'₌','('=>'₍',')'=>'₎','+'=>'₊','-'=>'₋'}
- @@rot_hash =
{}
Class Method Summary collapse
-
.chr_uni_esc(num) ⇒ Object
self.inside_int_cmp.
- .define_bracket_pair(str) ⇒ Object
-
.inside_int_cmp(mode = true) ⇒ Object
ary.sort &String::inside_int_cmp.
-
.random_password(chars = 8, special = "_-#!~@$%^*+=?:") ⇒ Object
generator does not need an instance.
- .reset_bracket_pairs ⇒ Object
- .undefine_bracket_pair(str) ⇒ Object
Instance Method Summary collapse
-
#access_mode(md = :stop, dflt = ' ') ⇒ Object
default char for blank? or new mode.
- #append(*prms) ⇒ Object
- #append!(*prms) ⇒ Object
-
#case_match(other) ⇒ Object
non-alpha chars must match too.
- #clean?(rgx = R_RATED) ⇒ Boolean
-
#cmp(other) ⇒ Object
replace with this …
- #cmpi(other) ⇒ Object
- #compact(sl = SET_COMPACT_LEFT, sr = SET_COMPACT_RIGHT) ⇒ Object
- #compact!(sl = SET_COMPACT_LEFT, sr = SET_COMPACT_RIGHT) ⇒ Object
- #condense ⇒ Object
- #condense! ⇒ Object
- #cross_match(pat) ⇒ Object
-
#cryptogram(dat = nil) ⇒ Object
nil==> encode, string==>test for match.
- #dec(val = nil) ⇒ Object
- #dec!(val = nil) ⇒ Object
- #downcase? ⇒ Boolean
- #duplicates ⇒ Object
- #duplicates? ⇒ Boolean
- #each_ord ⇒ Object
- #enclose(pairs, escape = nil, set = String::STD_ESCAPE_SET_RUBY, hash = String::STD_ESCAPE_HASH) ⇒ Object
- #enclose!(pairs, escape = nil) ⇒ Object
- #eqli?(other) ⇒ Boolean
- #equali?(other) ⇒ Boolean
- #extract(prm) ⇒ Object
- #extract!(prm = 1, fill_hole = "") ⇒ Object
- #extract_leading_set!(set) ⇒ Object
- #extract_num! ⇒ Object
- #extract_trailing_int! ⇒ Object
- #extract_trailing_set!(set) ⇒ Object
-
#find_all(obj, *modes) ⇒ Object
returns an array of all positions of obj in self.
- #find_near(pos, obj = String::SET_SPLIT_CHARS, *flags) ⇒ Object
- #find_nth(obj, nth, *modes) ⇒ Object
- #first ⇒ Object
-
#first! ⇒ Object
removes first char.
- #gs_titlecase ⇒ Object
- #gs_titlecase! ⇒ Object
- #histogram ⇒ Object
- #ignore_me ⇒ Object
- #inc(val = nil) ⇒ Object
-
#inc!(val = nil) ⇒ Object
increments integer at end of string.
- #indent ⇒ Object
- #indent! ⇒ Object
- #index(search, from = 0, *options) ⇒ Object (also: #find)
-
#keep(ks) ⇒ Object
only keep chars belonging to set.
- #keep!(ks) ⇒ Object
- #last ⇒ Object
-
#last! ⇒ Object
removes last char.
-
#like(table_class_name, table_column_name, mode_and = true, kp = SET_LIKE_KEEP) ⇒ Object
————————————————-.
- #limit_to(size, *flags) ⇒ Object
- #limit_to!(size, *flags) ⇒ Object
- #mixedcase? ⇒ Boolean
- #next ⇒ Object
-
#old_string_index_method_4gstring ⇒ Object
## Updates to existing index, rindex methods # now accepts search types of: Set, Array # ##.
- #old_string_rindex_method_4gstring ⇒ Object
-
#padto(tar, *prms) ⇒ Object
- ::todo
-
Add flags :rotate, :stick.
- #padto!(tar, *flags) ⇒ Object
- #parse(search_key = String::SET_PARSE_CHARS, *options) ⇒ Object
- #parsed ⇒ Object
-
#php_substr(pos = 0, len = nil) ⇒ Object
negative len means position from right to left positive len means number of chars to capture exception if pos and len are both negative and pos > len …
- #pop(nchars = 1) ⇒ Object
- #prev ⇒ Object
- #push(str) ⇒ Object
- #rand(cnt = 1, *prms) ⇒ Object
-
#rand!(cnt = 1, *prms) ⇒ Object
once is implied.
-
#rehash ⇒ Object
more consistent hash method.
-
#remove(obj, *prm) ⇒ Object
remove all characters.
-
#remove!(obj, *prm) ⇒ Object
remove all characters.
- #rindex(search, from = -1,, *options) ⇒ Object (also: #rfind)
- #rot_crypt(pswd = "Standard Password should never be used!") ⇒ Object
- #rot_decrypt(pswd = "Standard Password should never be used!") ⇒ Object
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
- #sort ⇒ Object
- #sort! ⇒ Object
- #split_on_set(set = ' ') ⇒ Object
- #sublist(*prms) ⇒ Object
- #sublist!(*prms) ⇒ Object
- #swapchar(pos, ch, *modes) ⇒ Object
- #to_crlf ⇒ Object
- #to_crlf! ⇒ Object
- #to_eng(pa = 6, unit = nil) ⇒ Object
-
#to_lf ⇒ Object
convert rn to n.
-
#to_lf! ⇒ Object
removes r.
-
#to_num ⇒ Object
convert to integer or float depending on format.
- #to_scd(dp = nil, delim = ',.') ⇒ Object
- #to_subscript(html = false) ⇒ Object
- #to_superscript(html = false) ⇒ Object
- #uchr ⇒ Object
-
#unenclose(escape = nil) ⇒ Object
unescape.
- #unenclose!(escape = nil) ⇒ Object
- #upcase? ⇒ Boolean
- #wrap_to(len, *flags) ⇒ Object
-
#zap! ⇒ Object
simple alias.
Class Method Details
.chr_uni_esc(num) ⇒ Object
self.inside_int_cmp
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gstring.rb', line 119 def self.chr_uni_esc(num) num = num.to_i return nil if num < 0 if num < 256 rtn = "\\x" + num.to_s(16).padto(2,'0',:left) elsif num < 0x10000 rtn = "\\u" + num.to_s(16).padto(4,'0',:left) elsif num < 0x1000000 rtn = "\\u" + num.to_s(16).padto(6,'0',:left) else rtn = "\\u" + num.to_s(16).padto(8,'0',:left) end end |
.define_bracket_pair(str) ⇒ Object
75 76 77 |
# File 'lib/gstring.rb', line 75 def self.define_bracket_pair(str) @@gs_bracketing_pairs[str.first] = str.last end |
.inside_int_cmp(mode = true) ⇒ Object
ary.sort &String::inside_int_cmp
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/gstring.rb', line 80 def self.inside_int_cmp(mode=true) return lambda do |a,b| a,b = (mode) ? [a.to_s, b.to_s] : [b.to_s, a.to_s] if(a==b) 0 else ta = a.dup tb = b.dup rgx = /\d+/ if (ta.find(rgx).nil? || tb.find(rgx).nil?) a <=> b # standard compare else # int inside one or both rtn=0 loop do if(ta==tb) rtn=0 break end if(ta.empty? || tb.empty?) rtn = ta <=> tb break end la = ta.parse(rgx, :no_skip, :no_strip) lb = tb.parse(rgx, :no_skip, :no_strip) if(la != lb) rtn = la <=> lb break end if(ta.parsed != tb.parsed) rtn = ta.parsed.to_i <=> tb.parsed.to_i break end end #loop rtn end # if end # if end # lambda end |
.random_password(chars = 8, special = "_-#!~@$%^*+=?:") ⇒ Object
generator does not need an instance
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/gstring.rb', line 581 def self.random_password(chars=8, special="_-#!~@$%^*+=?:") raise "password must be at least 8 characters" if chars < 8 low = BitSet.lowercase_chars high = BitSet.uppercase_chars digits = BitSet.digit_chars special = special.to_bset rescue BitSet.new all = low | high | digits | special a,b = low.rand(2,:array_chars) c,d = high.rand(2, :array_chars) e = digits.rand(1, :array_chars) f = special.rand(1, :array_chars) pswd = [a,b,c,d,e] pswd.push f unless (f.nil? || f.empty?) filler = all.rand(chars - pswd.length, :array_chars) filler.each do |ch| pswd.push ch end pswd.shuffle! return pswd.join '' end |
.reset_bracket_pairs ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/gstring.rb', line 61 def self.reset_bracket_pairs @@gs_bracketing_pairs = { '[' => ']', '(' => ')', '{' => '}', '<' => '>' } end |
.undefine_bracket_pair(str) ⇒ Object
71 72 73 |
# File 'lib/gstring.rb', line 71 def self.undefine_bracket_pair(str) @@gs_bracketing_pairs.delete(str.first) end |
Instance Method Details
#access_mode(md = :stop, dflt = ' ') ⇒ Object
default char for blank? or new mode
1644 1645 1646 1647 1648 1649 1650 |
# File 'lib/gstring.rb', line 1644 def access_mode(md=:stop, dflt=' ') # :swing :stick :default :rotate @gs_access_mode = md @gs_default_str = dflt @gs_np_pos = nil @gs_dir = :up self end |
#append(*prms) ⇒ Object
1103 1104 1105 1106 1107 1108 1109 |
# File 'lib/gstring.rb', line 1103 def append(*prms) rtn = "" prms.each do |item| rtn += item.to_s end self + rtn end |
#append!(*prms) ⇒ Object
1111 1112 1113 |
# File 'lib/gstring.rb', line 1111 def append!(*prms) replace self.append(*prms) end |
#case_match(other) ⇒ Object
non-alpha chars must match too
224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/gstring.rb', line 224 def case_match(other) # true if string same size with same case at all positions return false if (length != other.length) length.times do |idx| if (self[idx].upcase?) return false unless other[idx].upcase? elsif (self[idx].downcase?) return false unless other[idx].downcase? else # both must be letters at the position return false if self[idx] != other[idx] end end return true end |
#clean?(rgx = R_RATED) ⇒ Boolean
199 200 201 |
# File 'lib/gstring.rb', line 199 def clean?(rgx = R_RATED) return self.scan(rgx).empty? end |
#cmp(other) ⇒ Object
replace with this …
564 565 566 |
# File 'lib/gstring.rb', line 564 def cmp(other) self <=> other end |
#cmpi(other) ⇒ Object
568 569 570 |
# File 'lib/gstring.rb', line 568 def cmpi(other) self.upcase <=> other.upcase end |
#compact(sl = SET_COMPACT_LEFT, sr = SET_COMPACT_RIGHT) ⇒ Object
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 |
# File 'lib/gstring.rb', line 1450 def compact(sl=SET_COMPACT_LEFT, sr=SET_COMPACT_RIGHT) #sl = sl.to_bset #sr = sr.to_bset rtn = "" state = :start each_char do |ch| if :start == state next if ch <= ' ' state = state = sr.include?(ch) ? :right : :add rtn += ch elsif :add == state if ch <= ' ' state = :zap rtn += ' ' elsif sr.include? ch state = :right rtn += ch else rtn += ch end elsif :right == state unless ch <= ' ' rtn += ch state = sr.include?(ch) ? :right : :add end else #elsif :zap == state next if ch <= ' ' rtn.pop if sl.include? ch # state = :add state = sr.include?(ch) ? :right : :add rtn += ch end end rtn.pop if rtn.last==' ' return rtn end |
#compact!(sl = SET_COMPACT_LEFT, sr = SET_COMPACT_RIGHT) ⇒ Object
1488 1489 1490 |
# File 'lib/gstring.rb', line 1488 def compact!(sl=SET_COMPACT_LEFT, sr=SET_COMPACT_RIGHT) replace compact(sl, sr) end |
#condense ⇒ Object
409 410 411 |
# File 'lib/gstring.rb', line 409 def condense dup.condense! end |
#condense! ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/gstring.rb', line 393 def condense! strip! str = "" sp = false each_char do |ch| if (ch <= ' ') sp = true else str += sp ? ' ' + ch : ch sp = false end end replace str self end |
#cross_match(pat) ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/gstring.rb', line 345 def cross_match(pat) s_ptr=0 p_ptr=0 ch = '' return nil unless pat.class==String return false if empty? return false if pat.empty? loop do return true if s_ptr >= self.length return false if p_ptr >= pat.length if ch != '' if ch==self[s_ptr] ch="" s_ptr +=1 p_ptr +=1 if s_ptr >= self.length return false if pat[p_ptr]=='+' end else s_ptr +=1 return false if s_ptr >= self.length # ran out before match end elsif (pat[p_ptr]=='_') p_ptr+=1 s_ptr+=1 elsif (pat[p_ptr]=='+') s_ptr+=1 # skip next letter p_ptr+=1 ch = pat[p_ptr] return true if (ch.nil? || ch=='') #end of the line elsif (pat[p_ptr]=='*') p_ptr+=1 ch = pat[p_ptr] return true if (ch.nil? || ch=='') #end of the line elsif (pat[p_ptr]=='`') p_ptr+=1 return false unless pat[p_ptr]==self[s_ptr] p_ptr+=1 s_ptr+=1 else # compare chars return false unless pat[p_ptr]==self[s_ptr] p_ptr+=1 s_ptr+=1 end end return true end |
#cryptogram(dat = nil) ⇒ Object
nil==> encode, string==>test for match
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/gstring.rb', line 304 def cryptogram(dat=nil) # nil==> encode, string==>test for match if (dat.nil?) rtn = dup set = BitSet.lowercase_chars off_limits = [] skey = nil ary = (self.downcase.to_bset & String::SET_LOWERS).to_a(false).shuffle loop do break if ary.empty? skey = ary.pop hits = find_all(skey,:ignore) - off_limits off_limits = (off_limits | hits).sort # edge case, set only has 1 element in it rpw = (set.count==1) ? set.to_s : (set - skey).rand(1,:string) set -= rpw #can only use an element once hits.each do |pos| rtn.swapchar(pos, rpw, :casehold) end end return rtn elsif (dat.class==String) return false if self.length != dat.length s1 = self.downcase.to_bset & BitSet.lowercase_chars s2 = dat.downcase.to_bset & BitSet.lowercase_chars return false if s1.count != s2.count ary1 = self.downcase.find_all(s1) ary2 = dat.downcase.find_all(s2) return false if ary1 != ary2 return false unless case_match(dat) length.times do |idx| # ::TODO:: find faster way ... ary1 = self.find_all(self[idx],:ignore) ary2 = dat.find_all(dat[idx],:ignore) return false if ary1 != ary2 end return true else return nil end nil end |
#dec(val = nil) ⇒ Object
1080 1081 1082 |
# File 'lib/gstring.rb', line 1080 def dec(val=nil) return dup.dec!(val) end |
#dec!(val = nil) ⇒ Object
1070 1071 1072 1073 1074 1075 1076 1077 1078 |
# File 'lib/gstring.rb', line 1070 def dec!(val=nil) num = extract_trailing_int! return self if num.nil? return self if 0==num val ||= 1 return self if (num-val) < 0 append!(num-val) dup end |
#downcase? ⇒ Boolean
210 211 212 213 214 215 |
# File 'lib/gstring.rb', line 210 def downcase? set = self.to_bset return false if String::SET_UPPERS ** set # may not have any lower return nil unless String::SET_LOWERS ** set # must have at least one lower return true end |
#duplicates ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/gstring.rb', line 452 def duplicates str = self.sort set = BitSet.new rtn = "" last_chr = "" str.each_char do |ch| if last_chr == ch unless set.include? ch rtn += ch end set.add! ch end last_chr = ch end return rtn end |
#duplicates? ⇒ Boolean
446 447 448 449 450 |
# File 'lib/gstring.rb', line 446 def duplicates? set = self.to_bset return false if set.count == length return true end |
#each_ord ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/gstring.rb', line 480 def each_ord unless block_given? enu = Enumerator.new do |y| self.each_char do |ch| y << ch.ord end end return enu end self.each_char do |ch| yield ch.ord end end |
#enclose(pairs, escape = nil, set = String::STD_ESCAPE_SET_RUBY, hash = String::STD_ESCAPE_HASH) ⇒ Object
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
# File 'lib/gstring.rb', line 712 def enclose(pairs, escape=nil, set=String::STD_ESCAPE_SET_RUBY, hash=String::STD_ESCAPE_HASH) return self if pairs.empty? if escape.nil? return pairs.first + self + pairs.last else # look for pairs.first , and replace with {escape}{pairs.first} str = pairs.first self.each_char do |ch| if set.include? ch idx = (set & ch).to_a.first if (ch ** (set-[0..31])) #byebug str += "\\" + ch elsif hash.include? idx #byebug str += hash[idx] else # use hex format str += String.chr_uni_esc(idx) end else str += ch end end return str + pairs.last #str = self.gsub(pairs.first) { escape + pairs.first } #return pairs.first + str + pairs.last end end |
#enclose!(pairs, escape = nil) ⇒ Object
740 741 742 |
# File 'lib/gstring.rb', line 740 def enclose!(pairs, escape=nil) replace enclose(pairs, escape) end |
#eqli?(other) ⇒ Boolean
572 573 574 |
# File 'lib/gstring.rb', line 572 def eqli?(other) self.upcase == other.upcase end |
#equali?(other) ⇒ Boolean
576 577 578 |
# File 'lib/gstring.rb', line 576 def equali?(other) self.upcase == other.upcase end |
#extract(prm) ⇒ Object
708 709 710 |
# File 'lib/gstring.rb', line 708 def extract(prm) return dup.extract!(prm, '') end |
#extract!(prm = 1, fill_hole = "") ⇒ Object
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
# File 'lib/gstring.rb', line 635 def extract!(prm=1, fill_hole="") return "" if empty? if prm.kind_of? Integer # extract fron-end of string return "" if prm < 1 prm = prm > length ? length : prm rtn = self[0..(prm-1)] # replace( fill_hole + self[prm..-1] ) # WRONG! ... we only want the same number of fill replace( fill_hole[0..(prm-1)] + self[prm..-1] ) return rtn elsif prm.class == Range start = prm.first < 0 ? self.length + prm.first : prm.first finish = prm.last < 0 ? self.length + prm.last : prm.last if (start <= finish) # normal forward order return "" if start >= length finish = finish >= length ? length-1 : finish return extract!(finish+1, fill_hole) if start==0 # was extract without the '!' rtn = self[start..finish] replace(self[0..(start-1)] + fill_hole + self[(finish+1)..-1]) return rtn else # reverse order return "" if finish >= length start = start >= length ? length - 1 : start rtn = self[finish..start].reverse if(finish==0) replace(fill_hole + self[(start+1)..-1]) else replace(self[0..(finish-1)] + fill_hole + self[(start+1)..-1]) end return rtn end elsif prm.class == Array rtn = "" # first count number of substitutions cnt = 0 prm.each do |item| if item.kind_of? Integer cnt += 1 else cnt += item.count end end filler = fill_hole.padto(cnt, "\000", :right, :no_trunc) # use null as place holder prm.each do |item| if item.kind_of? Integer pos = item < 0 ? 0 : item if pos < length rtn += self[pos] self[pos]=filler.first! end # ignore if out of range else # use recursion str = self.extract!(item, filler) rtn += str filler.extract! str.length # remove and discard end end remove! "\000".to_bset return rtn else # convert to set ary = (prm.to_bset & (0..(self.length-1))).to_a #ignore everything out of range fill=fill_hole.dup rtn = "" oft = 0 ary.each do |idx| ch = fill.extract! rtn += self[idx-oft] self[idx-oft]=ch oft+=1 if ch.empty? end return rtn end return "" end |
#extract_leading_set!(set) ⇒ Object
1123 1124 1125 1126 1127 1128 1129 1130 |
# File 'lib/gstring.rb', line 1123 def extract_leading_set!(set) rtn = "" set = set.to_bset while set.include? first do rtn += first! end return rtn end |
#extract_num! ⇒ Object
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
# File 'lib/gstring.rb', line 1375 def extract_num! dat = parse(String::RGX_FLOAT, :no_skip) if parsed.nil? # no number found num = dat.extract_leading_set!(BitSet.digit_chars) replace dat return 0 if num.empty? return num.to_i else return parsed.to_f end end |
#extract_trailing_int! ⇒ Object
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'lib/gstring.rb', line 1059 def extract_trailing_int! return nil if empty? return nil unless String::SET_INT_CHARS.include? last str = "" while String::SET_INT_CHARS.include? last str += last! break if empty? end return str.reverse.to_i end |
#extract_trailing_set!(set) ⇒ Object
1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/gstring.rb', line 1132 def extract_trailing_set!(set) rtn = "" set = set.to_bset while set.include? last do rtn += last! end return rtn.reverse end |
#find_all(obj, *modes) ⇒ Object
returns an array of all positions of obj in self
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/gstring.rb', line 262 def find_all(obj, *modes) # returns an array of all positions of obj in self pos = 0 ary = [] loop do pos = self.index(obj, pos, *modes) break if pos.nil? ary.push pos pos +=1 end return ary end |
#find_near(pos, obj = String::SET_SPLIT_CHARS, *flags) ⇒ Object
274 275 276 277 278 |
# File 'lib/gstring.rb', line 274 def find_near(pos, obj=String::SET_SPLIT_CHARS, *flags) p_r = self.index(obj, pos, *flags) p_l = self.rindex(obj, pos, *flags) return [p_l,p_r] end |
#find_nth(obj, nth, *modes) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/gstring.rb', line 280 def find_nth(obj, nth, *modes) return nil if 0==nth if nth > 0 pos = 0 loop do nth -= 1 pos = self.index(obj, pos, *modes) return nil if pos.nil? return pos if nth.zero? pos += 1 end else nth = -nth pos = length loop do nth -= 1 pos = self.rindex(obj, pos, *modes) return nil if pos.nil? return pos if nth.zero? pos -= 1 end end end |
#first ⇒ Object
1028 1029 1030 1031 |
# File 'lib/gstring.rb', line 1028 def first return "" if empty? self[0] end |
#first! ⇒ Object
removes first char
1039 1040 1041 1042 1043 1044 |
# File 'lib/gstring.rb', line 1039 def first! return "" if empty? rtn = self[0] replace self[1..(length-1)] return rtn end |
#gs_titlecase ⇒ Object
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
# File 'lib/gstring.rb', line 1163 def gs_titlecase downcase! if index(GS_SENTENCE_TERM) ary = self.split_on_set(GS_SENTENCE_TERM) pa = [] ary.first.each do |sentence| pa.push(sentence.gs_titlecase.strip) end ary[0] = pa.reverse! ary[1].reverse! rtn = ary.first.pop until ary.first.empty? rtn += "#{ary[1].pop} #{ary.first.pop}" end rtn += ary.last || "" return rtn end ary = self.split(' ') ary.first.capitalize! ary.last.capitalize! (1..(ary.size-2)).each do |idx| ary[idx].capitalize! unless GS_TITLE_EXCEPTIONS.include?(ary[idx]) end return ary.join ' ' end |
#gs_titlecase! ⇒ Object
1189 1190 1191 |
# File 'lib/gstring.rb', line 1189 def gs_titlecase! replace gs_titlecase end |
#histogram ⇒ Object
438 439 440 441 442 443 444 |
# File 'lib/gstring.rb', line 438 def histogram hash = {} self.split('').sort.each do |ch| hash[ch] = hash[ch].nil? ? 1 : 1 + hash[ch] end return hash end |
#ignore_me ⇒ Object
854 855 856 |
# File 'lib/gstring.rb', line 854 def ignore_me self end |
#inc(val = nil) ⇒ Object
1099 1100 1101 |
# File 'lib/gstring.rb', line 1099 def inc(val=nil) return dup.inc!(val) end |
#inc!(val = nil) ⇒ Object
increments integer at end of string
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'lib/gstring.rb', line 1085 def inc!(val=nil) num = extract_trailing_int! if num.nil? if val.nil? append!(0) else append!(val) end else append! (val.nil? ? (num+1) : (num+val)) end dup end |
#indent ⇒ Object
765 766 767 768 769 770 771 772 |
# File 'lib/gstring.rb', line 765 def indent cnt = 0 each_char do |ch| break if ch > ' ' cnt += 1 end cnt end |
#indent! ⇒ Object
774 775 776 777 778 |
# File 'lib/gstring.rb', line 774 def indent! rtn = indent lstrip! return rtn end |
#index(search, from = 0, *options) ⇒ Object Also known as: find
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 |
# File 'lib/gstring.rb', line 948 def index(search,from=0,*) while .first.class==Array = .first end if (search.class==String) return self.downcase.index(search.downcase,from) if (.include? :ignore) return old_string_index_method_4gstring(search,from) end if search.class==Array best = nil search.each_with_index do |item,idx| pos = index(item,from,) unless pos.nil? @best ||= idx best ||= pos if pos < best best = pos @best = idx end end end return best # parse uses this to know which item was found end # call this for unrecognized options return old_string_index_method_4gstring(search,from) unless search.class == BitSet if .include? :ignore return self.downcase.index(search.add_opposing_case,from) end if (from < 0) from = length + from end from = 0 if from < 0 ((from)...(length)).each do |ptr| return ptr if search.include? self[ptr] end return nil end |
#keep(ks) ⇒ Object
only keep chars belonging to set
623 624 625 626 627 628 629 |
# File 'lib/gstring.rb', line 623 def keep(ks) # only keep chars belonging to set rtn = "" self.each_char do |ch| rtn += ch if ks.include? ch end return rtn end |
#keep!(ks) ⇒ Object
631 632 633 |
# File 'lib/gstring.rb', line 631 def keep!(ks) replace keep(ks) end |
#last ⇒ Object
1033 1034 1035 1036 |
# File 'lib/gstring.rb', line 1033 def last return "" if empty? self[length-1] end |
#last! ⇒ Object
removes last char
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
# File 'lib/gstring.rb', line 1047 def last! if length==1 rtn = dup self.clear return rtn end return "" if empty? rtn = self[length-1] replace self[0..(length-2)] return rtn end |
#like(table_class_name, table_column_name, mode_and = true, kp = SET_LIKE_KEEP) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/gstring.rb', line 173 def like(table_class_name, table_column_name, mode_and=true, kp = SET_LIKE_KEEP) ary = [] self.split.each do |substr| str = substr.keep kp if (str.first=='*') str.first! if (str.last=='*') str.last! ary.push "%#{str}%".gsub('*', '%') else ary.push "%#{str}".gsub('*', '%') end elsif (str.last=='*') str.last! ary.push "#{str}%".gsub('*', '%') else ary.push "%#{str}%".gsub('*', '%') end end #each md = mode_and ? ' and ' : ' or ' qry = (["`#{table_column_name}` like ?"] * ary.count).join md return table_class_name.where(qry, *ary) end |
#limit_to(size, *flags) ⇒ Object
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 |
# File 'lib/gstring.rb', line 1563 def limit_to(size, *flags) size = length + size + 1 if size < 0 if flags.first.kind_of? Integer flags=flags.dup cnt=flags.reverse!.pop pos = find_nth(SET_VERTICLE,cnt,*flags) if pos && (pos < size) size = pos + 1 end end return self if length <= size if flags.include? :no_break pos = self.rfind(SET_SPLIT_CHARS,size-1) return self[0..(size-2)] + '…' if pos.nil? return self[0..(pos-1)] + '…' else return self[0..(size-2)] + '…' end end |
#limit_to!(size, *flags) ⇒ Object
1583 1584 1585 |
# File 'lib/gstring.rb', line 1583 def limit_to!(size, *flags) replace limit_to(size, *flags) end |
#mixedcase? ⇒ Boolean
217 218 219 220 221 |
# File 'lib/gstring.rb', line 217 def mixedcase? set = self.to_bset return nil unless set ** (String::SET_LOWERS | String::SET_UPPERS) # must have a letter return (set ** String::SET_LOWERS) && (set ** String::SET_UPPERS) end |
#next ⇒ Object
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 |
# File 'lib/gstring.rb', line 1652 def next @gs_np_pos ||= nil @gs_access_mode ||= :stop @gs_dir ||= :up return "" if empty? if @gs_np_pos.nil? ch = first @gs_np_pos = 0 return ch else case @gs_access_mode when :stop @gs_np_pos = @gs_np_pos >= length ? length : @gs_np_pos + 1 ch = self[@gs_np_pos] return ch.nil? ? "" : ch when :rotate @gs_np_pos += 1 @gs_np_pos = @gs_np_pos >= length ? 0 : @gs_np_pos return self[@gs_np_pos] when :stick @gs_np_pos = @gs_np_pos >= length ? length : @gs_np_pos + 1 ch = self[@gs_np_pos] return ch.nil? ? last : ch when :default @gs_np_pos = @gs_np_pos >= length ? length : @gs_np_pos + 1 ch = self[@gs_np_pos] return ch.nil? ? @gs_default_str : ch when :swing return first if length==1 if @gs_dir== :up @gs_np_pos += 1 if @gs_np_pos >= length @gs_dir= :down @gs_np_pos = length - 2 end return self[@gs_np_pos] else # :down @gs_np_pos -= 1 if @gs_np_pos < 0 @gs_dir= :up @gs_np_pos = 1 end return self[@gs_np_pos] end else raise "unsupported access mode" end end end |
#old_string_index_method_4gstring ⇒ Object
##
Updates to existing index, rindex methods #
now accepts search types of: Set, Array #
##
945 |
# File 'lib/gstring.rb', line 945 alias_method :old_string_index_method_4gstring, :index |
#old_string_rindex_method_4gstring ⇒ Object
946 |
# File 'lib/gstring.rb', line 946 alias_method :old_string_rindex_method_4gstring, :rindex |
#padto(tar, *prms) ⇒ Object
- ::todo
-
Add flags :rotate, :stick
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 |
# File 'lib/gstring.rb', line 782 def padto(tar, *prms) return self if length == tar prms = prms.first if prms.first.kind_of? Array with = ' ' flags = [] how = :left prms.each do |prm| if prm.kind_of? String with=prm next end case prm when :no_trunc, :rotate, :stick, :space, :swing flags.push prm when :left, :right, :both how = prm else raise "padto unknown parameter" end end if length > tar return self if flags.include? :no_trunc return "" if tar<=0 return '…' if tar==1 return self[0..(tar-2)]+'…' end with = ' ' if with.empty? rtn = "" flags -= [:no_trunc] flag = flags.empty? ? :stick : flags.first with.access_mode(flag==:space ? :default : flag) case how when :right rtn = self (tar-length).times do |xx| rtn += with.next end when :both lp = (tar-length) >> 1 rp = (tar-length) - lp str = "" (rp).times do |xx| str += with.next end rtn = str[0..(lp-1)].reverse + self + str else # :left str = "" (tar-length).times do |xx| str += with.next end rtn += str.reverse + self end rtn end |
#padto!(tar, *flags) ⇒ Object
837 838 839 |
# File 'lib/gstring.rb', line 837 def padto!(tar, *flags) replace padto(tar, flags) end |
#parse(search_key = String::SET_PARSE_CHARS, *options) ⇒ Object
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
# File 'lib/gstring.rb', line 858 def parse(search_key = String::SET_PARSE_CHARS, *) = .first if .first.class==Array meth = .include?(:no_strip) ? :ignore_me : :strip rtn="" @found = nil if(search_key.class==BitSet) #skip over first char idx = .include?(:no_skip) ? 0 : 1 sk = .include?(:ignore) ? search_key.add_opposing_case : search_key sf = idx self[sf...length].each_char do |ch| if(sk.include? ch) @found=ch break end idx += 1 end unless @found.nil? rtn = (idx==0) ? "" : self[0..(idx-1)].send(meth) replace self[(idx+1)..-1].send(meth) return rtn end rtn = clone clear return rtn elsif (search_key.class == Regexp) sf = .include?(:no_skip) ? 0 : 1 pos = index(search_key,sf) @found = pos.nil? ? nil : self.match(search_key)[0] if pos.nil? rtn = clone clear return rtn else # found rtn = pos.zero? ? "" : self[0..(pos-1)].send(meth) replace self[(pos+@found.length)..length].send(meth) return rtn end elsif (search_key.class == String) #skip over first char unless option specifies :no_skip sf = .include?(:no_skip) ? 0 : 1 idx = index(search_key,sf,) if idx.nil? rtn = clone clear return rtn else rtn = idx.zero? ? "" : self[0..(idx-1)].send(meth) @found = self[idx..(idx+search_key.length-1)] replace self[(idx+search_key.length)..length].send(meth) return rtn end elsif (search_key.class == Array) shortest = nil best = nil start = .include?(:no_skip) ? 0 : 1 search_key.each_with_index do |val,idx| pos = self.index(val,start,) unless pos.nil? best = idx if best.nil? shortest ||= pos if (pos < shortest) shortest = pos best = idx end end end if shortest.nil? rtn = clone clear return rtn else #opt = options.dup #opt[:no_skip]=true #return parse(search_key[best],opt) return parse(search_key[best],) end else # we are passed something that should have been converted to a set return parse([search_key].to_bset,) end end |
#parsed ⇒ Object
1119 1120 1121 |
# File 'lib/gstring.rb', line 1119 def parsed @found ||= nil end |
#php_substr(pos = 0, len = nil) ⇒ Object
negative len means position from right to left positive len means number of chars to capture exception if pos and len are both negative and pos > len … then starting point is the |sum| from the right, and length … and the ending point is also the |sum| ??? need more examples
846 847 848 849 850 851 852 |
# File 'lib/gstring.rb', line 846 def php_substr(pos=0,len=nil) return "" if len==0 len = len.nil? ? length : len r_s = (pos >= 0) ? pos : pos + length r_e = (len >= 0) ? r_s + len - 1 : len + length - 1 return self[(r_s)..(r_e)] end |
#pop(nchars = 1) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/gstring.rb', line 417 def pop(nchars=1) if (nchars < 1) # avoid silly requests return "" elsif (nchars >= length) rtn = dup replace "" return rtn end rtn = self[-nchars..-1] replace self[0..(length-nchars-1)] return rtn end |
#prev ⇒ Object
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 |
# File 'lib/gstring.rb', line 1702 def prev @gs_np_pos ||= nil @gs_access_mode ||= :stop @gs_dir ||= :up return "" if empty? if @gs_np_pos.nil? ch = last @gs_np_pos = length - 1 return ch else case @gs_access_mode when :stop @gs_np_pos = @gs_np_pos <= 0 ? -1 : @gs_np_pos - 1 return "" if @gs_np_pos < 0 return self[@gs_np_pos] when :rotate @gs_np_pos -= 1 @gs_np_pos = @gs_np_pos < 0 ? length-1 : @gs_np_pos return self[@gs_np_pos] when :stick @gs_np_pos = @gs_np_pos <= 0 ? -1 : @gs_np_pos - 1 return first if @gs_np_pos < 0 return self[@gs_np_pos] when :default @gs_np_pos = @gs_np_pos <= 0 ? -1 : @gs_np_pos - 1 return @gs_default_str if @gs_np_pos < 0 return self[@gs_np_pos] when :swing return first if length==1 if @gs_dir== :down @gs_np_pos += 1 if @gs_np_pos >= length @gs_dir= :up @gs_np_pos = length - 2 end return self[@gs_np_pos] else # :up @gs_np_pos -= 1 if @gs_np_pos < 0 @gs_dir= :down @gs_np_pos = 1 end return self[@gs_np_pos] end else raise "unsupported access mode" end end end |
#push(str) ⇒ Object
413 414 415 |
# File 'lib/gstring.rb', line 413 def push(str) replace(self+str).dup end |
#rand(cnt = 1, *prms) ⇒ Object
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 |
# File 'lib/gstring.rb', line 1603 def rand(cnt=1, *prms) return "" if empty? return "" if cnt < 1 if length==1 return self if prms.include? :set return self * cnt end rnd = Random.new rtn = "" str = prms.include?(:set) ? self.to_bset.to_s : self.dup cnt.times do break if str.empty? ch = str[rnd.rand(str.length)] rtn += ch str[str.find(ch)] = "" if prms.include? :once end return rtn end |
#rand!(cnt = 1, *prms) ⇒ Object
once is implied
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 |
# File 'lib/gstring.rb', line 1622 def rand!(cnt=1, *prms) # once is implied return "" if empty? return "" if cnt < 1 if length==1 return first! end rnd = Random.new rtn = "" cnt.times do break if self.empty? ch = self[rnd.rand(self.length)] rtn += ch if prms.include? :set remove! ch.to_bset else self[self.find(ch)] = "" end end return rtn end |
#rehash ⇒ Object
more consistent hash method
494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/gstring.rb', line 494 def rehash # more consistent hash method sum = 0 tr = 4294967296 rL = Random.new(self.length) sum = rL.rand(tr) self.each_char do |ch| rt = Random.new(ch.ord) sum += rt.rand(tr) sum ^= rL.rand(sum) end return sum & (tr-1) end |
#remove(obj, *prm) ⇒ Object
remove all characters
617 618 619 620 621 |
# File 'lib/gstring.rb', line 617 def remove(obj, *prm) # remove all characters str = dup str.remove!(obj, *prm) return str end |
#remove!(obj, *prm) ⇒ Object
remove all characters
602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/gstring.rb', line 602 def remove!(obj, *prm) # remove all characters flags = prm.dup flags |= [:no_skip, :no_strip] rtn = "" str = "" loop do str += parse(obj,flags) break if parsed.nil? rtn += parsed end str += self replace str return rtn end |
#rindex(search, from = -1,, *options) ⇒ Object Also known as: rfind
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/gstring.rb', line 988 def rindex(search,from=-1,*) = .first if .first.class==Array if (search.class==String) return self.downcase.rindex(search.downcase,from) if (.include? :ignore) return old_string_rindex_method_4gstring(search,from) end if search.class==Array best = nil search.each_with_index do |item,idx| pos = rindex(item,from,) unless pos.nil? @best ||= idx best ||= pos if pos > best best = pos @best = idx end end end return best # parse uses this to know which item was found end # call this for unrecognized options return old_string_rindex_method_4gstring(search,from) unless search.class == BitSet if .include? :ignore return self.downcase.rindex(search.add_opposing_case,from) end idx = (from < 0) ? length + from : from return nil if idx < 0 until (search.include? (self[idx])) do idx -= 1 return nil if idx < 0 end return idx end |
#rot_crypt(pswd = "Standard Password should never be used!") ⇒ Object
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/gstring.rb', line 507 def rot_crypt(pswd="Standard Password should never be used!") raise "illegal password" if pswd.empty? rtn = "" ptr = 0 rnd = Random.new(pswd.rehash) len = pswd.length self.each_char do |ch| ss = @@rot_hash[ch] if ss.nil? # make no subs rtn += ch else # pos = (ch.ord + pswd[ptr].ord + rnd.rand(ROT_LEN)) % ROT_LEN pos = (ss + pswd[ptr].ord + rnd.rand(ROT_LEN)) % ROT_LEN rtn += ROT_STRING[pos] end ptr += 1 ptr = 0 if ptr >= len end return rtn end |
#rot_decrypt(pswd = "Standard Password should never be used!") ⇒ Object
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/gstring.rb', line 528 def rot_decrypt(pswd="Standard Password should never be used!") raise "illegal password" if pswd.empty? rtn = "" ptr = 0 rnd = Random.new(pswd.rehash) len = pswd.length self.each_char do |ch| ss = @@rot_hash[ch] if ss.nil? # make no subs rtn += ch else pos = @@rot_hash[ch] # got back the number pos = pos - pswd[ptr].ord - rnd.rand(ROT_LEN) pos = pos % ROT_LEN # rtn += pos.chr(Encoding::UTF_8) rtn += ROT_STRING[pos] end ptr += 1 ptr = 0 if ptr >= len end return rtn end |
#shuffle ⇒ Object
1193 1194 1195 |
# File 'lib/gstring.rb', line 1193 def shuffle return self.split('').shuffle.join end |
#shuffle! ⇒ Object
1197 1198 1199 |
# File 'lib/gstring.rb', line 1197 def shuffle! replace shuffle end |
#sort ⇒ Object
430 431 432 |
# File 'lib/gstring.rb', line 430 def sort self.split('').sort.join('') end |
#sort! ⇒ Object
434 435 436 |
# File 'lib/gstring.rb', line 434 def sort! replace self.split('').sort.join('') end |
#split_on_set(set = ' ') ⇒ Object
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 |
# File 'lib/gstring.rb', line 1141 def split_on_set(set=' ') str = dup if (set.nil? || set.empty?) set = ' ' end set = set.to_bset prefx = str.extract_leading_set!(set) pstfx = str.extract_trailing_set!(set) dat = [] tok = [] loop do break if str.empty? dat.push str.parse(set, :no_strip) tmp = str.parsed break if tmp.nil? tmp += str.extract_leading_set!(set) tok.push tmp end dat.push str unless str.empty? return [dat,tok,prefx,pstfx] end |
#sublist(*prms) ⇒ Object
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 |
# File 'lib/gstring.rb', line 1201 def sublist(*prms) if (prms.first.class==Symbol) if prms.first == :bang prms = prms[1] else raise "illegal sublist parameter" end end prms.reverse! cls = prms.pop flags = {} dflt = nil subh = nil # substitution hash suba = nil # substitution array cnt = nil until prms.empty? tmp = prms.pop if(tmp.class==Array) raise "parameter error" unless suba.nil? suba = tmp.reverse # make it easy to pop, and makes a new instance elsif (tmp.class==Hash) raise "parameter error" unless subh.nil? subh = {} tmp.each do |key,val| subh[key.to_s] = val.to_s # convert keys and values to strings end elsif (tmp.class==Symbol) flags[tmp] = true elsif (tmp.class==Fixnum) raise "count parameter repeated" unless cnt.nil? cnt = tmp elsif (tmp.class==String) raise "parameter error" unless dflt.nil? dflt = tmp else raise "unknown parameter type" end end str = self.dup ptr = 0 rtn = "" cls = [cls] if (String==cls.class) fa = [] # add more as needed later ... fa.push :ignore if flags.include? :ignore fass = fa + [:no_strip] + [:no_skip] cls = [cls] if (BitSet==cls.class) # push everything into an array even regx cls = [cls] if (Regexp==cls.class) flst = (flags.include? :first) ? {} : nil # remove nil values subh ||= {} suba ||= [] dflt ||= "" if flags.include? :ignore # update substitution hash keys to lower case if :ignore unless subh.empty? tmp = {} subh.each do |key,val| tmp[key.downcase] = val.to_s end subh = tmp end end unless (Hash==cls.class) loop do unless cnt.nil? if (cnt <= 0) rtn += str return rtn end end rtn += str.parse(cls, fass) # make sure we are working on arrays of a bunch of stuff including nested === add test case break if str.parsed.nil? unless flst.nil? # process first flag exceptions key = (flags.include? :ignore) ? str.parsed.downcase : str.parsed if flst.include? key rtn += str.parsed # make no substitution cnt -= 1 unless cnt.nil? next else flst[key] = true # mark as taboo on future substitutions end end # first check to see if we have a hash match unless subh.empty? key = (flags.include? :ignore) ? str.parsed.downcase : str.parsed dat = subh[key] unless dat.nil? rtn += dat return rtn if str.empty? cnt -= 1 unless cnt.nil? next end end # next check to see if we have substitution data in the array unless suba.empty? rtn += suba.pop return rtn if str.empty? cnt -= 1 unless cnt.nil? next end # at last use the default if flags.include? :stop # do not make substitutions with default rtn += str.parsed rtn += str return rtn elsif flags.include? :skip # skip over, then keep going rtn += str.parsed else rtn += dflt end return rtn if str.empty? cnt -= 1 unless cnt.nil? end # loop return rtn else # (Hash==cls.class) cls = cls.dup loop do unless cnt.nil? if (cnt <= 0) rtn += str return rtn end end pos = nil best = nil bval = nil cls.each do |key,val| idx = str.index(key.to_s, 0 , fa) unless idx.nil? pos ||= idx best ||= key.to_s bval ||= val.to_s if(idx < pos) pos = idx best = key.to_s bval = val.to_s end break if idx.zero? # can't get better than zero, so stop looking end end if best.nil? rtn += str return rtn end # byebug if flags.include? :ignore rtn += str.parse(best, fass) break if str.parsed.nil? if flags.include? :first cls.delete best cls.delete best.to_sym # just in case end rtn += bval cnt -= 1 unless cnt.nil? end #loop return rtn end end |
#sublist!(*prms) ⇒ Object
1367 1368 1369 |
# File 'lib/gstring.rb', line 1367 def sublist!(*prms) replace sublist(:bang, prms) end |
#swapchar(pos, ch, *modes) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/gstring.rb', line 238 def swapchar(pos, ch, *modes) modes = modes.first if modes.first.class==Array rtn = self[pos] return nil if rtn.nil? if ch.empty? return nil unless modes.include? :empty_ok end unless ch.empty? ch = ch.first unless modes.include? :substring end if modes.include? :casehold if rtn.upcase? self[pos]=ch.upcase elsif rtn.downcase? self[pos]=ch.downcase else self[pos]=ch.first end else self[pos]=ch # standard mode end rtn end |
#to_crlf ⇒ Object
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 |
# File 'lib/gstring.rb', line 1387 def to_crlf rtn = "" state = :normal each_char do |ch| if :normal == state if "\r" == ch state = :r_pending elsif "\n" == ch rtn += "\r\n" else rtn += ch end else if "\n" == ch rtn += "\r\n" else rtn += "\r" rtn += ch end state = :normal end end return rtn end |
#to_crlf! ⇒ Object
1437 1438 1439 1440 |
# File 'lib/gstring.rb', line 1437 def to_crlf! self.replace to_crlf self end |
#to_eng(pa = 6, unit = nil) ⇒ Object
1587 1588 1589 |
# File 'lib/gstring.rb', line 1587 def to_eng(pa=6, unit=nil) return self.to_f.to_eng(pa, unit) end |
#to_lf ⇒ Object
convert rn to n
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
# File 'lib/gstring.rb', line 1412 def to_lf # convert \r\n to \n rtn = "" state = :normal each_char do |ch| if :normal == state if "\r" == ch state = :r_pending elsif "\n" == ch rtn += "\012" else rtn += ch end else if "\n" == ch rtn += "\n" else rtn += "\r" rtn += ch end state = :normal end end return rtn end |
#to_lf! ⇒ Object
removes r
1442 1443 1444 1445 |
# File 'lib/gstring.rb', line 1442 def to_lf! # removes \r self.replace to_lf self end |
#to_num ⇒ Object
convert to integer or float depending on format
1371 1372 1373 |
# File 'lib/gstring.rb', line 1371 def to_num # convert to integer or float depending on format self.dup.extract_num! end |
#to_scd(dp = nil, delim = ',.') ⇒ Object
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 |
# File 'lib/gstring.rb', line 1591 def to_scd(dp=nil, delim = ',.') num = to_num if(dp.nil?) if num.class==Float return num.to_scd(2,delim) else return num.to_scd(0,delim) end end return num.to_scd(dp,delim) end |
#to_subscript(html = false) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/gstring.rb', line 162 def to_subscript(html=false) return '<sub>' + self + '</sub>' if html rtn = "" each_char do |ch| rtn += String::SET_SUB_CHARS.include?(ch) ? @@gs_sub_hash[ch] : ch end return rtn end |
#to_superscript(html = false) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/gstring.rb', line 153 def to_superscript(html=false) return '<sup>' + self + '</sup>' if html rtn = "" each_char do |ch| rtn += String::SET_SUP_CHARS.include?(ch) ? @@gs_sup_hash[ch] : ch end return rtn end |
#uchr ⇒ Object
1752 1753 1754 |
# File 'lib/gstring.rb', line 1752 def uchr first end |
#unenclose(escape = nil) ⇒ Object
unescape
744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/gstring.rb', line 744 def unenclose(escape=nil) # unescape return nil if length < 2 unless first==last return nil unless last == @@gs_bracketing_pairs[first] end if @@gs_bracketing_pairs.include? first return nil if first==last end str = self[1..(length-2)] unless escape.nil? str = str.gsub(escape) { '' } # blind removal? ... may need to rethink this a tad end return str end |
#unenclose!(escape = nil) ⇒ Object
759 760 761 762 763 |
# File 'lib/gstring.rb', line 759 def unenclose!(escape=nil) str = unenclose(escape) replace str unless str.nil? str.nil? ? nil : self end |
#upcase? ⇒ Boolean
203 204 205 206 207 208 |
# File 'lib/gstring.rb', line 203 def upcase? set = self.to_bset return false if String::SET_LOWERS ** set # may not have any lower return nil unless String::SET_UPPERS ** set # must have at least one upper return true end |
#wrap_to(len, *flags) ⇒ Object
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 |
# File 'lib/gstring.rb', line 1494 def wrap_to(len, *flags) # remove this as cr will mess things up # the logic below will need to be inferred as needed #if (self.length < len) # if flags.include? :array # return [self.dup] # else # return self.dup # end #end ary = [] str = self.to_lf # remove messy \r if present if flags.include? :approximate loop do tar = str.find_near(len) tmp = str.find("\n") unless tmp.nil? tar = [tmp,tmp] if tmp <= len end # now for the nasty end points and edge cases if tar.first.nil? if tar.last.nil? ary.push str.extract!(len) else ary.push str.extract!(tar.last + 1) end else if tar.last.nil? ary.push str.extract!(tar.first + 1) else # find closest fit if (len - tar.first) <= (tar.last - len) ary.push str.extract!(tar.first + 1) else ary.push str.extract!(tar.last + 1) end end end break if str.length <= len end # loop else loop do tar = str.rindex(String::SET_SPLIT_CHARS, len) tmp = str.find("\n") unless tmp.nil? tar = tmp if tmp <= len end if (tar.nil?) ary.push str.extract!(len) else ary.push str.extract!(tar+1) end break if str.length <= len end #loop end #if ary.push str ary.length.times { |ii| ary[ii].strip! } unless flags.include? :no_strip return ary if flags.include? :array if flags.include? :html str = ary.join '<br />' else str = ary.join "\n" end return str end |
#zap! ⇒ Object
simple alias
1115 1116 1117 |
# File 'lib/gstring.rb', line 1115 def zap! # simple alias clear end |