Method: String#succ
- Defined in:
- lib/source/ruby.rb
#succ ⇒ Object
call-seq:
str.next -> string
str.succ -> string
Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case.
If the increment generates a “carry,” the character to the left is also incremented. This process repeats until there is no carry, adding an additional character of the same type as the leftmost alphanumeric, if necessary.
'abcdef'.succ #=> 'abcdeg'
'<<kit9>>'.succ #=> '<<kit10>>'
'1999zzz'.succ #=> '2000aaa'
'ZZZ9999'.succ #=> 'AAAA0000'
6002 6003 6004 6005 6006 6007 6008 6009 |
# File 'lib/source/ruby.rb', line 6002 def succ `var v=this.__value__` `if(!/[a-zA-Z0-9]/.test(v)){return $q(v);}` `if(/^\\d+$/.test(v)){return $q(''+(+v+1))}` `for(var i=v.length-1,carry=i>=0,result='';i>=0;--i){var c=v[i],lc=/[a-z]/.test(c),uc=/[A-Z]/.test(c),n=/[0-9]/.test(c);if($T(carry)&&(lc||uc||n)){if(lc||uc){if(c=='z'||c=='Z'){result=(lc?'a':'A')+result;carry=i;}else{result=String.fromCharCode(c.charCodeAt()+1)+result;carry=false;};}else{if(c=='9'){result='0'+result;carry=i}else{result=''+(+c+1)+result;carry=false;};};}else{result=c+result;};}` `if($T(carry)){var c=v[carry],insert=/[a-z]/.test(c)?'a':(/[A-Z]/.test(c)?'A':'1');result=result.slice(0,carry)+insert+result.slice(carry,result.length);}` return `$q(result)` end |