|
@@ -22,7 +22,7 @@
|
|
/**
|
|
/**
|
|
This class provides advanced methods on Strings. It is ideally used with
|
|
This class provides advanced methods on Strings. It is ideally used with
|
|
'using StringTools' and then acts as an extension to the String class.
|
|
'using StringTools' and then acts as an extension to the String class.
|
|
-
|
|
|
|
|
|
+
|
|
If the first argument to any of the methods is null, the result is
|
|
If the first argument to any of the methods is null, the result is
|
|
unspecified.
|
|
unspecified.
|
|
**/
|
|
**/
|
|
@@ -83,7 +83,7 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Escapes HTML special characters of the string [s].
|
|
Escapes HTML special characters of the string [s].
|
|
-
|
|
|
|
|
|
+
|
|
The following replacements are made:
|
|
The following replacements are made:
|
|
- & becomes &
|
|
- & becomes &
|
|
- < becomes <
|
|
- < becomes <
|
|
@@ -99,10 +99,10 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Unescapes HTML special characters of the string [s].
|
|
Unescapes HTML special characters of the string [s].
|
|
-
|
|
|
|
|
|
+
|
|
This is the inverse operation to htmlEscape, i.e. the following always
|
|
This is the inverse operation to htmlEscape, i.e. the following always
|
|
holds: htmlUnescape(htmlEscape(s)) == s
|
|
holds: htmlUnescape(htmlEscape(s)) == s
|
|
-
|
|
|
|
|
|
+
|
|
The replacements follow:
|
|
The replacements follow:
|
|
- & becomes &
|
|
- & becomes &
|
|
- < becomes <
|
|
- < becomes <
|
|
@@ -116,9 +116,9 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Tells if the string [s] starts with the string [start].
|
|
Tells if the string [s] starts with the string [start].
|
|
-
|
|
|
|
|
|
+
|
|
If [start] is null, the result is unspecified.
|
|
If [start] is null, the result is unspecified.
|
|
-
|
|
|
|
|
|
+
|
|
If [start] is the empty String "", the result is true.
|
|
If [start] is the empty String "", the result is true.
|
|
**/
|
|
**/
|
|
public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
|
|
public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
|
|
@@ -133,9 +133,9 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Tells if the string [s] ends with the string [end].
|
|
Tells if the string [s] ends with the string [end].
|
|
-
|
|
|
|
|
|
+
|
|
If [end] is null, the result is unspecified.
|
|
If [end] is null, the result is unspecified.
|
|
-
|
|
|
|
|
|
+
|
|
If [end] is the empty String "", the result is true.
|
|
If [end] is the empty String "", the result is true.
|
|
**/
|
|
**/
|
|
public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
|
|
public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
|
|
@@ -152,10 +152,10 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Tells if the character in the string [s] at position [pos] is a space.
|
|
Tells if the character in the string [s] at position [pos] is a space.
|
|
-
|
|
|
|
|
|
+
|
|
A character is considered to be a space character if its character code
|
|
A character is considered to be a space character if its character code
|
|
is 9,10,11,12,13 or 32.
|
|
is 9,10,11,12,13 or 32.
|
|
-
|
|
|
|
|
|
+
|
|
If [s] is the empty String "", or if pos is not a valid position within
|
|
If [s] is the empty String "", or if pos is not a valid position within
|
|
[s], the result is false.
|
|
[s], the result is false.
|
|
**/
|
|
**/
|
|
@@ -166,10 +166,10 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Removes leading space characters of [s].
|
|
Removes leading space characters of [s].
|
|
-
|
|
|
|
|
|
+
|
|
This function internally calls isSpace() to decide which characters to
|
|
This function internally calls isSpace() to decide which characters to
|
|
remove.
|
|
remove.
|
|
-
|
|
|
|
|
|
+
|
|
If [s] is the empty String "" or consists only of space characters, the
|
|
If [s] is the empty String "" or consists only of space characters, the
|
|
result is the empty String "".
|
|
result is the empty String "".
|
|
**/
|
|
**/
|
|
@@ -191,10 +191,10 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Removes trailing space characters of [s].
|
|
Removes trailing space characters of [s].
|
|
-
|
|
|
|
|
|
+
|
|
This function internally calls isSpace() to decide which characters to
|
|
This function internally calls isSpace() to decide which characters to
|
|
remove.
|
|
remove.
|
|
-
|
|
|
|
|
|
+
|
|
If [s] is the empty String "" or consists only of space characters, the
|
|
If [s] is the empty String "" or consists only of space characters, the
|
|
result is the empty String "".
|
|
result is the empty String "".
|
|
**/
|
|
**/
|
|
@@ -217,7 +217,7 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Removes leading and trailing space characters of [s].
|
|
Removes leading and trailing space characters of [s].
|
|
-
|
|
|
|
|
|
+
|
|
This is a convenience function for ltrim(rtrim(s)).
|
|
This is a convenience function for ltrim(rtrim(s)).
|
|
**/
|
|
**/
|
|
public #if (cs || java) inline #end static function trim( s : String ) : String {
|
|
public #if (cs || java) inline #end static function trim( s : String ) : String {
|
|
@@ -229,23 +229,23 @@ class StringTools {
|
|
return ltrim(rtrim(s));
|
|
return ltrim(rtrim(s));
|
|
#end
|
|
#end
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
Concatenates [c] to [s] until [s].length is at least [l].
|
|
Concatenates [c] to [s] until [s].length is at least [l].
|
|
-
|
|
|
|
|
|
+
|
|
If [c] is the empty String "" or if [l] does not exceed [s].length,
|
|
If [c] is the empty String "" or if [l] does not exceed [s].length,
|
|
[s] is returned unchanged.
|
|
[s] is returned unchanged.
|
|
-
|
|
|
|
|
|
+
|
|
If [c].length is 1, the resulting String length is exactly [l].
|
|
If [c].length is 1, the resulting String length is exactly [l].
|
|
-
|
|
|
|
|
|
+
|
|
Otherwise the length may exceed [l].
|
|
Otherwise the length may exceed [l].
|
|
-
|
|
|
|
|
|
+
|
|
If [c] is null, the result is unspecified.
|
|
If [c] is null, the result is unspecified.
|
|
**/
|
|
**/
|
|
public static function lpad( s : String, c : String, l : Int ) : String {
|
|
public static function lpad( s : String, c : String, l : Int ) : String {
|
|
if (c.length <= 0)
|
|
if (c.length <= 0)
|
|
return s;
|
|
return s;
|
|
-
|
|
|
|
|
|
+
|
|
while (s.length < l) {
|
|
while (s.length < l) {
|
|
s = c + s;
|
|
s = c + s;
|
|
}
|
|
}
|
|
@@ -254,20 +254,20 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Appends [c] to [s] until [s].length is at least [l].
|
|
Appends [c] to [s] until [s].length is at least [l].
|
|
-
|
|
|
|
|
|
+
|
|
If [c] is the empty String "" or if [l] does not exceed [s].length,
|
|
If [c] is the empty String "" or if [l] does not exceed [s].length,
|
|
[s] is returned unchanged.
|
|
[s] is returned unchanged.
|
|
-
|
|
|
|
|
|
+
|
|
If [c].length is 1, the resulting String length is exactly [l].
|
|
If [c].length is 1, the resulting String length is exactly [l].
|
|
-
|
|
|
|
|
|
+
|
|
Otherwise the length may exceed [l].
|
|
Otherwise the length may exceed [l].
|
|
-
|
|
|
|
|
|
+
|
|
If [c] is null, the result is unspecified.
|
|
If [c] is null, the result is unspecified.
|
|
**/
|
|
**/
|
|
public static function rpad( s : String, c : String, l : Int ) : String {
|
|
public static function rpad( s : String, c : String, l : Int ) : String {
|
|
if (c.length <= 0)
|
|
if (c.length <= 0)
|
|
return s;
|
|
return s;
|
|
-
|
|
|
|
|
|
+
|
|
while (s.length < l) {
|
|
while (s.length < l) {
|
|
s = s + c;
|
|
s = s + c;
|
|
}
|
|
}
|
|
@@ -277,17 +277,20 @@ class StringTools {
|
|
/**
|
|
/**
|
|
Replace all occurences of the String [sub] in the String [s] by the
|
|
Replace all occurences of the String [sub] in the String [s] by the
|
|
String [by].
|
|
String [by].
|
|
-
|
|
|
|
|
|
+
|
|
If [sub] is the empty String "", [by] is inserted after each character
|
|
If [sub] is the empty String "", [by] is inserted after each character
|
|
of [s]. If [by] is also the empty String "", [s] remains unchanged.
|
|
of [s]. If [by] is also the empty String "", [s] remains unchanged.
|
|
-
|
|
|
|
|
|
+
|
|
This is a convenience function for [s].split([sub]).join([by]).
|
|
This is a convenience function for [s].split([sub]).join([by]).
|
|
-
|
|
|
|
|
|
+
|
|
If [sub] or [by] are null, the result is unspecified.
|
|
If [sub] or [by] are null, the result is unspecified.
|
|
**/
|
|
**/
|
|
- public #if (java || cs) inline #end static function replace( s : String, sub : String, by : String ) : String {
|
|
|
|
|
|
+ public #if cs inline #end static function replace( s : String, sub : String, by : String ) : String {
|
|
#if java
|
|
#if java
|
|
- return untyped s.replace(sub, by);
|
|
|
|
|
|
+ if (sub.length == 0)
|
|
|
|
+ return s.split(sub).join(by);
|
|
|
|
+ else
|
|
|
|
+ return untyped s.replace(sub, by);
|
|
#elseif cs
|
|
#elseif cs
|
|
return untyped s.Replace(sub, by);
|
|
return untyped s.Replace(sub, by);
|
|
#else
|
|
#else
|
|
@@ -297,7 +300,7 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Encodes [n] into a hexadecimal representation.
|
|
Encodes [n] into a hexadecimal representation.
|
|
-
|
|
|
|
|
|
+
|
|
If [digits] is specified, the resulting String is padded with "0" until
|
|
If [digits] is specified, the resulting String is padded with "0" until
|
|
its length equals [digits].
|
|
its length equals [digits].
|
|
**/
|
|
**/
|
|
@@ -322,11 +325,11 @@ class StringTools {
|
|
|
|
|
|
/**
|
|
/**
|
|
Returns the character code at position [index] of String [s].
|
|
Returns the character code at position [index] of String [s].
|
|
-
|
|
|
|
|
|
+
|
|
This method is faster than String.charCodeAt() on most platforms.
|
|
This method is faster than String.charCodeAt() on most platforms.
|
|
However, unlike String.charCodeAt(), the result is unspecified if
|
|
However, unlike String.charCodeAt(), the result is unspecified if
|
|
[index] is negative or exceeds [s].length.
|
|
[index] is negative or exceeds [s].length.
|
|
-
|
|
|
|
|
|
+
|
|
This operation is not guaranteed to work if [s] contains the \0
|
|
This operation is not guaranteed to work if [s] contains the \0
|
|
character. The method isEof() can be used to check for that.
|
|
character. The method isEof() can be used to check for that.
|
|
**/
|
|
**/
|
|
@@ -378,7 +381,7 @@ class StringTools {
|
|
#if java
|
|
#if java
|
|
private static inline function _charAt(str:String, idx:Int):java.StdTypes.Char16 return untyped str._charAt(idx);
|
|
private static inline function _charAt(str:String, idx:Int):java.StdTypes.Char16 return untyped str._charAt(idx);
|
|
#end
|
|
#end
|
|
-
|
|
|
|
|
|
+
|
|
#if neko
|
|
#if neko
|
|
private static var _urlEncode = neko.Lib.load("std","url_encode",1);
|
|
private static var _urlEncode = neko.Lib.load("std","url_encode",1);
|
|
private static var _urlDecode = neko.Lib.load("std","url_decode",1);
|
|
private static var _urlDecode = neko.Lib.load("std","url_decode",1);
|