StringExt.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cs.internal;
  23. import cs.internal.Function;
  24. private typedef NativeString = String;
  25. @:keep @:nativeGen @:native("haxe.lang.StringExt") private class StringExt
  26. {
  27. @:functionCode('
  28. if ( ((uint) index) >= me.Length)
  29. return "";
  30. else
  31. return new string(me[index], 1);
  32. ')
  33. public static function charAt(me:NativeString, index:Int):NativeString
  34. {
  35. return null;
  36. }
  37. @:functionCode('
  38. if ( ((uint) index) >= me.Length)
  39. return default(haxe.lang.Null<int>);
  40. else
  41. return new haxe.lang.Null<int>((int)me[index], true);
  42. ')
  43. public static function charCodeAt(me:NativeString, index:Int):Null<Int>
  44. {
  45. return null;
  46. }
  47. @:functionCode('
  48. uint sIndex = (startIndex.hasValue) ? ((uint) startIndex.@value) : 0;
  49. if (sIndex >= me.Length)
  50. return -1;
  51. return me.IndexOf(str, (int)sIndex);
  52. ')
  53. public static function indexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
  54. {
  55. return -1;
  56. }
  57. @:functionCode('
  58. int sIndex = (startIndex.hasValue) ? (startIndex.@value) : (me.Length - 1);
  59. if (sIndex >= me.Length)
  60. sIndex = me.Length - 1;
  61. else if (sIndex < 0)
  62. return -1;
  63. //TestBaseTypes.hx@133 fix
  64. if (startIndex.hasValue)
  65. {
  66. for(int i = sIndex; i >= 0; i--)
  67. {
  68. bool found = true;
  69. for(int j = 0; j < str.Length; j++)
  70. {
  71. if(me[i + j] != str[j])
  72. {
  73. found = false;
  74. break;
  75. }
  76. }
  77. if (found)
  78. return i;
  79. }
  80. return -1;
  81. } else {
  82. return me.LastIndexOf(str, sIndex);
  83. }
  84. ')
  85. public static function lastIndexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
  86. {
  87. return -1;
  88. }
  89. @:functionCode('
  90. string[] native;
  91. if (delimiter.Length == 0)
  92. {
  93. int len = me.Length;
  94. native = new string[len];
  95. for (int i = 0; i < len; i++)
  96. native[i] = new string(me[i], 1);
  97. } else {
  98. native = me.Split(new string[] { delimiter }, System.StringSplitOptions.None);
  99. }
  100. return new Array<object>(native);
  101. ')
  102. public static function split(me:NativeString, delimiter:NativeString):Array<NativeString>
  103. {
  104. return null;
  105. }
  106. @:functionCode('
  107. int meLen = me.Length;
  108. int targetLen = meLen;
  109. if (len.hasValue)
  110. {
  111. targetLen = len.@value;
  112. if (targetLen == 0)
  113. return "";
  114. if( pos != 0 && targetLen < 0 ){
  115. return "";
  116. }
  117. }
  118. if( pos < 0 ){
  119. pos = meLen + pos;
  120. if( pos < 0 ) pos = 0;
  121. } else if( targetLen < 0 ){
  122. targetLen = meLen + targetLen - pos;
  123. }
  124. if( pos + targetLen > meLen ){
  125. targetLen = meLen - pos;
  126. }
  127. if ( pos < 0 || targetLen <= 0 ) return "";
  128. return me.Substring(pos, targetLen);
  129. ')
  130. public static function substr(me:NativeString, pos:Int, ?len:Int):NativeString
  131. {
  132. return null;
  133. }
  134. @:functionCode('
  135. int endIdx;
  136. int len = me.Length;
  137. if ( !endIndex.hasValue ) {
  138. endIdx = len;
  139. } else if ( (endIdx = endIndex.@value) < 0 ) {
  140. endIdx = 0;
  141. } else if ( endIdx > len ) {
  142. endIdx = len;
  143. }
  144. if ( startIndex < 0 ) {
  145. startIndex = 0;
  146. } else if ( startIndex > len ) {
  147. startIndex = len;
  148. }
  149. if ( startIndex > endIdx ) {
  150. int tmp = startIndex;
  151. startIndex = endIdx;
  152. endIdx = tmp;
  153. }
  154. return me.Substring(startIndex, endIdx - startIndex);
  155. ')
  156. public static function substring(me:NativeString, startIndex:Int, ?endIndex:Int):NativeString
  157. {
  158. return null;
  159. }
  160. @:functionCode('
  161. return me.ToLower();
  162. ')
  163. public static function toLowerCase(me:NativeString):NativeString
  164. {
  165. return null;
  166. }
  167. @:functionCode('
  168. return me.ToUpper();
  169. ')
  170. public static function toUpperCase(me:NativeString):NativeString
  171. {
  172. return null;
  173. }
  174. public static function toNativeString(me:NativeString):NativeString
  175. {
  176. return me;
  177. }
  178. @:functionCode('
  179. return new string( (char) code, 1 );
  180. ')
  181. public static function fromCharCode(code:Int):NativeString
  182. {
  183. return null;
  184. }
  185. }
  186. @:keep @:nativeGen @:native('haxe.lang.StringRefl') private class StringRefl
  187. {
  188. public static var fields = ["length", "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring"];
  189. public static function handleGetField(str:NativeString, f:NativeString, throwErrors:Bool):Dynamic
  190. {
  191. switch(f)
  192. {
  193. case "length": return str.length;
  194. case "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring":
  195. return new Closure(str, f, 0);
  196. default:
  197. if (throwErrors)
  198. throw "Field not found: '" + f + "' in String";
  199. else
  200. return null;
  201. }
  202. }
  203. public static function handleCallField(str:NativeString, f:NativeString, args:Array<Dynamic>):Dynamic
  204. {
  205. var _args:Array<Dynamic> = [str];
  206. if (args == null)
  207. args = _args;
  208. else
  209. args = _args.concat(args);
  210. return Runtime.slowCallField(StringExt, f, args);
  211. }
  212. }