Skip to content

Instantly share code, notes, and snippets.

@scztt
Last active December 12, 2024 15:15
Show Gist options
  • Save scztt/537881ca2656618a35de416986d146ef to your computer and use it in GitHub Desktop.
Save scztt/537881ca2656618a35de416986d146ef to your computer and use it in GitHub Desktop.
unicode Strings
+String {
unicodeSize {
var size = this.size;
size = size - (this.select({ |c| c.ascii < 0 }).size / 3 * 2).asInteger;
^size;
}
unicodeIndex {
|index|
var i = 0;
var char = 0;
while { char < index } {
if (this[i].ascii < 0) {
i = i + 3;
} {
i = i + 1;
};
char = char + 1;
};
^i;
}
unicodeAt {
|at|
var i = this.unicodeIndex(at);
if (this[i].ascii < 0) {
^this[i..(i+2)]
} {
^this[i]
}
}
unicodeWrapAt {
|i|
^this.unicodeAt(i % this.unicodeSize)
}
unicodeCollect {
|func|
var i = 0, j = 0, size = this.size;
var replacement, result;
result = CollStream("");
while { i < size } {
if (this[i].ascii < 0) {
result.putString(func.value(this[i], j).asString);
i = i + 1;
} {
result.putString(func.value(this[i..(i+2)], j).asString);
i = i + 3;
};
j = j + 1;
}
^result.collection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment