StringImpl.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 python.internal;
  23. import python.internal.Internal;
  24. @:native("HxString")
  25. class StringImpl {
  26. @:ifFeature("dynamic_read.split", "anon_optional_read.split", "python.internal.StringImpl.split")
  27. public static inline function split(s:String, d:String) {
  28. return if (d == "") UBuiltins.list(s) else Syntax.callField(s, "split", d);
  29. }
  30. @:ifFeature("dynamic_read.charCodeAt", "anon_optional_read.charCodeAt", "python.internal.StringImpl.charCodeAt")
  31. public static function charCodeAt(s:String, index:Int) {
  32. return if (s == null || s.length == 0 || index < 0 || index >= s.length) null else UBuiltins.ord(Syntax.arrayAccess(s, index));
  33. }
  34. @:ifFeature("dynamic_read.charAt", "anon_optional_read.charAt", "python.internal.StringImpl.charAt")
  35. public static inline function charAt(s:String, index:Int) {
  36. return if (index < 0 || index >= s.length) "" else Syntax.arrayAccess(s, index);
  37. }
  38. @:ifFeature("dynamic_read.lastIndexOf", "anon_optional_read.lastIndexOf", "python.internal.StringImpl.lastIndexOf")
  39. public static inline function lastIndexOf(s:String, str:String, ?startIndex:Int):Int {
  40. if (startIndex == null) {
  41. return Syntax.callField(s, "rfind", str, 0, s.length);
  42. } else if(str == "") {
  43. var length = s.length;
  44. if(startIndex < 0) {
  45. startIndex = length + startIndex;
  46. if(startIndex < 0) startIndex = 0;
  47. }
  48. return startIndex > length ? length : startIndex;
  49. } else {
  50. var i = Syntax.callField(s, "rfind", str, 0, startIndex + 1);
  51. var startLeft = i == -1 ? UBuiltins.max(0, startIndex + 1 - str.length) : i + 1;
  52. var check = Syntax.callField(s, "find", str, startLeft, s.length);
  53. if (check > i && check <= startIndex) {
  54. return check;
  55. } else {
  56. return i;
  57. }
  58. }
  59. }
  60. @:ifFeature("dynamic_read.toUpperCase", "anon_optional_read.toUpperCase", "python.internal.StringImpl.toUpperCase")
  61. public static inline function toUpperCase(s:String) {
  62. return Syntax.callField(s, "upper");
  63. }
  64. @:ifFeature("dynamic_read.toLowerCase", "anon_optional_read.toLowerCase", "python.internal.StringImpl.toLowerCase")
  65. public static inline function toLowerCase(s:String) {
  66. return Syntax.callField(s, "lower");
  67. }
  68. @:ifFeature("dynamic_read.indexOf", "anon_optional_read.indexOf", "python.internal.StringImpl.indexOf")
  69. public static inline function indexOf(s:String, str:String, ?startIndex:Int) {
  70. if (startIndex == null)
  71. return Syntax.callField(s, "find", str);
  72. else
  73. return indexOfImpl(s, str, startIndex);
  74. }
  75. static function indexOfImpl(s:String, str:String, startIndex:Int) {
  76. if(str == "") {
  77. var length = s.length;
  78. if(startIndex < 0) {
  79. startIndex = length + startIndex;
  80. if(startIndex < 0) startIndex = 0;
  81. }
  82. return startIndex > length ? length : startIndex;
  83. }
  84. return Syntax.callField(s, "find", str, startIndex);
  85. }
  86. @:ifFeature("dynamic_read.toString", "anon_optional_read.toString", "python.internal.StringImpl.toString")
  87. public static inline function toString(s:String) {
  88. return s;
  89. }
  90. @:ifFeature("dynamic_read.length", "anon_optional_read.length", "python.internal.StringImpl.length")
  91. public static inline function get_length(s:String) {
  92. return UBuiltins.len(s);
  93. }
  94. @:ifFeature("dynamic_read.fromCharCode", "anon_optional_read.fromCharCode", "python.internal.StringImpl.fromCharCode")
  95. public static inline function fromCharCode(code:Int):String {
  96. #if doc_gen
  97. return "";
  98. #else
  99. return Syntax.callField('', "join", UBuiltins.map(UBuiltins.chr, cast [code])); // TODO: check cast
  100. #end
  101. }
  102. @:ifFeature("dynamic_read.substring", "anon_optional_read.substring", "python.internal.StringImpl.substring")
  103. public static function substring(s:String, startIndex:Int, ?endIndex:Int):String {
  104. if (startIndex < 0)
  105. startIndex = 0;
  106. if (endIndex == null) {
  107. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  108. } else {
  109. if (endIndex < 0)
  110. endIndex = 0;
  111. if (endIndex < startIndex) {
  112. return Syntax.arrayAccess(s, endIndex, startIndex);
  113. } else {
  114. return Syntax.arrayAccess(s, startIndex, endIndex);
  115. }
  116. }
  117. }
  118. @:ifFeature("dynamic_read.substr", "anon_optional_read.substr", "python.internal.StringImpl.substr")
  119. public static function substr(s:String, startIndex:Int, ?len:Int):String {
  120. if (len == null) {
  121. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  122. } else {
  123. if (len == 0)
  124. return "";
  125. if (startIndex < 0) {
  126. startIndex = s.length + startIndex;
  127. if (startIndex < 0)
  128. startIndex = 0;
  129. }
  130. return Syntax.arrayAccess(s, startIndex, startIndex + len);
  131. }
  132. }
  133. }