Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active August 29, 2015 14:13
Show Gist options
  • Save masakielastic/ed8ab2b836ca9b44a7b9 to your computer and use it in GitHub Desktop.
Save masakielastic/ed8ab2b836ca9b44a7b9 to your computer and use it in GitHub Desktop.
require('es6-shim');
function next_char_info(str, pos) {
var cp = str.codePointAt(pos);
var size = cp > 0xFFFF ? 2 : 1;
return {'cp': cp, 'size': size};
}
var str = '𠮷野家';
var length = str.length;
var pos = 0;
var info;
while (pos < length) {
info = next_char_info(str, pos);
console.log(str.substr(pos, info.size));
pos += info.size;
}
require('es6-shim');
function MyString(str) {
this.str = str;
this.length = str.length;
this.init = function() {
this.pos = 0;
this.cp = 0;
this.size = 0;
this.current = '';
};
this.init();
this.next = function() {
if (this.pos >= this.length) {
return false;
}
this.cp = this.str.codePointAt(this.pos);
this.size = this.cp > 0xFFFF ? 2 : 1;
this.current = this.str.substr(this.pos, this.size);
this.pos += this.size;
return true;
};
this.move = function(pos) {
this.pos = pos;
}
this.nextFrom = function(pos) {
this.pos = pos;
return this.next();
}
this.print_each_char = function() {
this.init();
while (this.next()) {
console.log(this.current);
}
};
}
var str = new MyString('𠮷野家');
str.print_each_char();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment