Strings and Runes in ActionScript
A string in ActionScript is a sequence of UTF-16 code units. ActionScript treats strings as immutable sequences of characters. The concept of a "rune" in Go is similar to a "character" in ActionScript, which is represented by a single UTF-16 code unit or a surrogate pair for characters outside the Basic Multilingual Plane.
```actionscript
package {
import flash.display.Sprite;
import flash.text.TextField;
public class StringsAndChars extends Sprite {
public function StringsAndChars() {
// 's' is a String assigned a literal value
// representing the word "hello" in the Thai language.
const s:String = "สวัสดี";
var output:TextField = new TextField();
output.width = 400;
output.height = 300;
addChild(output);
// This will produce the length of the string in UTF-16 code units
output.appendText("Len: " + s.length + "\n");
// This loop generates the hex values of all
// the UTF-16 code units in 's'.
for (var i:int = 0; i < s.length; i++) {
output.appendText(s.charCodeAt(i).toString(16) + " ");
}
output.appendText("\n");
// To count how many characters are in a string, we can use
// the String.length property. Note that this may not always
// correspond to the number of visible characters for strings
// containing surrogate pairs or combining characters.
output.appendText("Character count: " + s.length + "\n");
// ActionScript doesn't have a built-in way to iterate over
// Unicode code points, but we can create a simple function
// to do so
output.appendText("\nIterating over code points:\n");
var index:int = 0;
while (index < s.length) {
var charCode:Number = s.charCodeAt(index);
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
// This is a high surrogate, so we need to include the next code unit
var codePoint:Number = (charCode - 0xD800) * 0x400 +
(s.charCodeAt(index + 1) - 0xDC00) + 0x10000;
output.appendText("U+" + codePoint.toString(16).toUpperCase() +
" starts at " + index + "\n");
index += 2;
} else {
output.appendText("U+" + charCode.toString(16).toUpperCase() +
" starts at " + index + "\n");
index++;
}
}
// This demonstrates passing a character to a function
for (var j:int = 0; j < s.length; j++) {
examineChar(s.charAt(j));
}
}
private function examineChar(c:String):void {
// We can compare a character to a string literal directly
if (c == 't') {
trace("found tee");
} else if (c == 'ส') {
trace("found so sua");
}
}
}
}
This ActionScript code demonstrates various operations on strings and characters, similar to the original Go example. Here are some key points:
ActionScript strings are sequences of UTF-16 code units, which is different from Go’s UTF-8 encoding.
The
length
property of a string in ActionScript returns the number of UTF-16 code units, which may not always correspond to the number of visible characters.ActionScript doesn’t have a built-in way to iterate over Unicode code points, so we implemented a custom function to do this.
The
charCodeAt()
method returns the UTF-16 code unit at a given index, similar to indexing into a string in Go.ActionScript doesn’t have a direct equivalent to Go’s runes, but we can work with individual characters using the
charAt()
method.We use a TextField to display the output, as ActionScript is typically used in a graphical environment.
This example provides a similar exploration of strings and characters as the original Go code, adapted to ActionScript’s features and conventions.