StringImpl.hx 5.5 KB

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