StringExt.hx 3.9 KB

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