StringExt.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C)2005-2019 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 jvm;
  23. import java.NativeString;
  24. @:native("haxe.jvm.StringExt")
  25. class StringExt {
  26. public static function fromCharCode(code:Int):String {
  27. var a = new java.NativeArray(1);
  28. a[0] = code;
  29. return new String(a, 0, 1);
  30. }
  31. public static function charAt(me:String, index:Int):String {
  32. if (index >= me.length || index < 0)
  33. return "";
  34. else
  35. return java.lang.Character._toString((cast me : NativeString).charAt(index));
  36. }
  37. public static function charCodeAt(me:String, index:Int):Null<Int> {
  38. if (index >= me.length || index < 0)
  39. return null;
  40. else
  41. return cast((cast me : NativeString).charAt(index), Int);
  42. }
  43. public static function indexOf(me:String, str:String, startIndex:Null<Int>) {
  44. return if (startIndex == null) (cast me : NativeString).indexOf(str) else (cast me : NativeString).indexOf(str, startIndex);
  45. }
  46. public static function lastIndexOf(me:String, str:String, ?startIndex:Int):Int {
  47. if (startIndex == null || startIndex > me.length || startIndex < 0) {
  48. startIndex = me.length - 1;
  49. }
  50. return (cast me : NativeString).lastIndexOf(str, startIndex);
  51. }
  52. public static function split(me:String, delimiter:String):Array<String> {
  53. var ret = [];
  54. if (delimiter.length == 0) {
  55. for (i in 0...me.length) {
  56. ret.push(me.charAt(i));
  57. }
  58. } else {
  59. var start = 0;
  60. var pos = me.indexOf(delimiter, start);
  61. while (pos >= 0) {
  62. ret.push((cast me : NativeString).substring(start, pos));
  63. start = pos + delimiter.length;
  64. pos = me.indexOf(delimiter, start);
  65. }
  66. ret.push((cast me : NativeString).substring(start));
  67. }
  68. return ret;
  69. }
  70. public static function substr(me:String, pos:Int, ?len:Int):String {
  71. var len:Int = len == null ? me.length : len;
  72. if (pos != 0 && len < 0) {
  73. return "";
  74. }
  75. if (pos < 0) {
  76. pos = me.length + pos;
  77. if (pos < 0) {
  78. pos = 0;
  79. }
  80. } else if (len < 0) {
  81. len = me.length + len - pos;
  82. }
  83. if (pos + len > me.length) {
  84. len = me.length - pos;
  85. }
  86. if (pos < 0 || len <= 0) {
  87. return "";
  88. }
  89. return (cast me : NativeString).substring(pos, pos + len);
  90. }
  91. public static function substring(me:String, startIndex:Int, ?endIndex:Int):String {
  92. var endIndex:Int = endIndex == null ? me.length : endIndex;
  93. if (endIndex < 0) {
  94. endIndex = 0;
  95. } else if (endIndex > me.length) {
  96. endIndex = me.length;
  97. }
  98. if (startIndex < 0) {
  99. startIndex = 0;
  100. } else if (startIndex > me.length) {
  101. startIndex = me.length;
  102. }
  103. if (startIndex > endIndex) {
  104. var tmp = startIndex;
  105. startIndex = endIndex;
  106. endIndex = tmp;
  107. }
  108. return (cast me : NativeString).substring(startIndex, endIndex);
  109. }
  110. public static function toLowerCase(me:String):String {
  111. return me.toLowerCase();
  112. }
  113. public static function toUpperCase(me:String):String {
  114. return me.toUpperCase();
  115. }
  116. }