package String import NoWurst import Integer import Annotations import Maths @configurable public constant ENABLE_MULTIBYTE_SUPPORT = false constant charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" constant numberset = "0123456789" constant numbersetlength = numberset.length() constant charsetlength = charset.length() /** Returns true if this string is non-null and not empty or only consisting of whitespace. */ public function string.isNotBlank() returns boolean return this != null and this.trim().length() > 0 /** returns the position of the char in the charset therefore converting a single char into an int that can be converted back to a char with .toCharsetString() */ public function string.toCharsetInt() returns int if this.length() > 1 return -1 else for i = 0 to charsetlength - 1 if charset.substring(i, i + 1) == this return i return -1 /** Creates a real from a string that only contains numbers (0-9) and a sign */ public function string.toReal() returns real return S2R(this) /** Creates an int from a string that only contains numbers (0-9) and a sign */ public function string.toInt() returns int return S2I(this) /** Converts an int to the representative char */ public function int.toCharsetString() returns string return charset.substring(this, this + 1) /** returns a substring specified by end and start position. */ public function string.substring(int start, int stop) returns string return SubString(this, start, stop) /** returns a substring specified by end and start position. */ public function string.substring(int start) returns string return SubString(this, start, this.length()) /** Length of the string */ public function string.length() returns int return StringLength(this) /** Returns the char at the given position. 0 = first char */ public function string.charAt(int index) returns string return SubString(this, index, index + 1) /** Converts a string to an ability id */ public function string.toAbilityId() returns int return AbilityId(this) /** Returns if the given string ends with the specified string. */ public function string.endsWith(string s) returns boolean if s == "" return true let len = this.length() if s.length() > len return false return this.substring(len - s.length(), len) == s /** Returns if the given string starts with the specified string. */ public function string.startsWith(string s) returns boolean let len = this.length() if s.length() > len return false return this.substring(0, s.length()) == s /** Returns the string in lowercase letters */ public function string.toLowerCase() returns string return StringCase(this, false) /** Returns the string in uppercase letters */ public function string.toUpperCase() returns string return StringCase(this, true) /** True when string contains only uppercase letters */ public function string.isUpper() returns boolean return this == StringCase(this, true) /** True when string contains only lowercase letters */ public function string.isLower() returns boolean return this == StringCase(this, false) /** Returns the given string with whitespaces removed from both ends*/ public function string.trim() returns string int i = 0 int j = this.length() while (i != j and this.charAt(i) == " ") i++ while (i != j and this.charAt(j - 1) == " ") j-- return this.substring(i, j) /** Returns the given string with whitespaces removed from the left end */ public function string.ltrim() returns string int i = 0 let length = this.length() while (i < length and this.charAt(i) == " ") i++ return this.substring(i, length) /** Returns the given string with whitespaces removed from the right end */ public function string.rtrim() returns string int j = this.length() while (j != 0 and this.charAt(j - 1) == " ") j-- return this.substring(0, j) /** Removes a string from the beginning of another string */ public function string.ltrim(string val) returns string if this.startsWith(val) return this.substring(val.length(), this.length()).ltrim(val) else return this /** Removes a string from the end of another string */ public function string.rtrim(string val) returns string if this.endsWith(val) return this.substring(0, this.length() - val.length()).rtrim(val) else return this /** Removes a string from both ends of another string */ public function string.trim(string val) returns string return this.ltrim(val).rtrim(val) /** Returns the index of the specified string inside the given string. If the string does not exist, the returnvalue is -1 */ public function string.indexOf(string s) returns int for int i = 0 to this.length() - s.length() if this.substring(i, i + s.length()) == s return i return -1 /** Returns the index of the specified string inside the given string, starting the search at given startposition. If the string does not exist, the returnvalue is -1 */ public function string.indexOf(string s, int startpos) returns int for int i = startpos to this.length() - s.length() if this.substring(i, i + s.length()) == s return i return -1 /** Returns the last index of the specified string inside the given string, If the string does not exist, the returnvalue is -1 */ public function string.lastIndexOf(string s) returns int for int i = this.length() - s.length() downto 0 if this.substring(i, i + s.length()) == s return i return -1 /** Returns the occurences of a certain string within a string */ public function string.countOccurences(string findStr) returns int int lastIndex = 0 int count = 0 while lastIndex != -1 lastIndex = this.indexOf(findStr, lastIndex) if lastIndex != -1 count++ lastIndex += findStr.length() return count /** True if the string contains the given string */ public function string.contains(string s) returns boolean return this.indexOf(s) != -1 /** Returns if the given string is a whitespace (" ",\n,\t,\r) */ public function string.isWhitespace() returns boolean return this == " " or this == "\n" or this == "\t" or this == "\r" /** Returns a new string with first letter is upper */ public function string.firstUpper() returns string if this == "" return "" return StringCase(this.substring(0, 1), true) + this.substring(1, this.length()) /** Returns a new string with first letter is lower */ public function string.firstLower() returns string if this == "" return "" return StringCase(this.substring(0, 1), false) + this.substring(1, this.length()) /** Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string */ public function string.replace(string oldSubstring, string newSubstring) returns string var s = this if oldSubstring.length() > 0 int i = this.indexOf(oldSubstring) while (i != -1) s = s.substring(0, i) + newSubstring + s.substring(i + oldSubstring.length(), s.length()) i = s.indexOf(oldSubstring, i + newSubstring.length()) return s /** Returns an integer result based on a lexicographic string comparison of strings s1 and s2. */ public function stringCompare(string s1, string s2) returns int let s1Len = s1.length() let s2Len = s2.length() for int i = 0 to min(s1Len, s2Len) let char1Int = s1.charAt(i).toCharsetInt() let char2Int = s2.charAt(i).toCharsetInt() if char1Int < char2Int return -1 else if char1Int > char2Int return 1 if s1Len < s2Len return -1 else if s1Len > s2Len return 1 return 0 public function string.getHash() returns int return StringHash(this) /** Formats the given string replacing {x} delimiters with the passed replacements. Example: "You got {0} gold".format(goldAmount) **/ public function string.format(vararg string replacements) returns string var result = this var i = 0 for replacement in replacements result = result.replace("{" + i.toString() + "}", replacement) i++ return result public function string.iterator() returns StringIterator return new StringIterator(this, 0, this.length()) public class StringIterator string s int currentpos int stringLen construct(string s, int currentpos, int stringLen) this.s = s this.currentpos = currentpos this.stringLen = stringLen function hasNext() returns boolean return currentpos < stringLen function next() returns string var val = s.substring(currentpos, currentpos + 1) if ENABLE_MULTIBYTE_SUPPORT and val.getHash() == 1843378377 val = s.substring(currentpos, currentpos + 2) currentpos += 2 else currentpos++ return val function close() destroy this public class StringLines string s int currentLine = 0 int lineCount int lastIndex construct(string s, int lastIndex, int lineCount) this.s = s this.lineCount = lineCount this.lastIndex = lastIndex function iterator() returns thistype return this function hasNext() returns boolean return currentLine < lineCount function next() returns string let idx = this.lastIndex this.lastIndex = this.s.indexOf("\n", idx) + 1 currentLine++ return this.s.substring(idx, this.lastIndex) function close() destroy this /** Returns a StringLines Object of the given string for iteration */ public function string.toLines() returns StringLines return new StringLines(this, 0, this.countOccurences("\n") + 1)