StringImpl.hx 5.0 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 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 {
  43. var i = Syntax.callField(s, "rfind", str, 0, startIndex + 1);
  44. var startLeft = i == -1 ? UBuiltins.max(0, startIndex + 1 - str.length) : i + 1;
  45. var check = Syntax.callField(s, "find", str, startLeft, s.length);
  46. if (check > i && check <= startIndex) {
  47. return check;
  48. } else {
  49. return i;
  50. }
  51. }
  52. }
  53. @:ifFeature("dynamic_read.toUpperCase", "anon_optional_read.toUpperCase", "python.internal.StringImpl.toUpperCase")
  54. public static inline function toUpperCase(s:String) {
  55. return Syntax.callField(s, "upper");
  56. }
  57. @:ifFeature("dynamic_read.toLowerCase", "anon_optional_read.toLowerCase", "python.internal.StringImpl.toLowerCase")
  58. public static inline function toLowerCase(s:String) {
  59. return Syntax.callField(s, "lower");
  60. }
  61. @:ifFeature("dynamic_read.indexOf", "anon_optional_read.indexOf", "python.internal.StringImpl.indexOf")
  62. public static inline function indexOf(s:String, str:String, ?startIndex:Int) {
  63. if (startIndex == null)
  64. return Syntax.callField(s, "find", str);
  65. else
  66. return Syntax.callField(s, "find", str, startIndex);
  67. }
  68. @:ifFeature("dynamic_read.toString", "anon_optional_read.toString", "python.internal.StringImpl.toString")
  69. public static inline function toString(s:String) {
  70. return s;
  71. }
  72. @:ifFeature("dynamic_read.length", "anon_optional_read.length", "python.internal.StringImpl.length")
  73. public static inline function get_length(s:String) {
  74. return UBuiltins.len(s);
  75. }
  76. @:ifFeature("dynamic_read.fromCharCode", "anon_optional_read.fromCharCode", "python.internal.StringImpl.fromCharCode")
  77. public static inline function fromCharCode(code:Int):String {
  78. #if doc_gen
  79. return "";
  80. #else
  81. return Syntax.callField('', "join", UBuiltins.map(UBuiltins.chr, cast [code])); // TODO: check cast
  82. #end
  83. }
  84. @:ifFeature("dynamic_read.substring", "anon_optional_read.substring", "python.internal.StringImpl.substring")
  85. public static function substring(s:String, startIndex:Int, ?endIndex:Int):String {
  86. if (startIndex < 0)
  87. startIndex = 0;
  88. if (endIndex == null) {
  89. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  90. } else {
  91. if (endIndex < 0)
  92. endIndex = 0;
  93. if (endIndex < startIndex) {
  94. return Syntax.arrayAccess(s, endIndex, startIndex);
  95. } else {
  96. return Syntax.arrayAccess(s, startIndex, endIndex);
  97. }
  98. }
  99. }
  100. @:ifFeature("dynamic_read.substr", "anon_optional_read.substr", "python.internal.StringImpl.substr")
  101. public static function substr(s:String, startIndex:Int, ?len:Int):String {
  102. if (len == null) {
  103. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  104. } else {
  105. if (len == 0)
  106. return "";
  107. if (startIndex < 0) {
  108. startIndex = s.length + startIndex;
  109. if (startIndex < 0)
  110. startIndex = 0;
  111. }
  112. return Syntax.arrayAccess(s, startIndex, startIndex + len);
  113. }
  114. }
  115. }