StringImpl.hx 4.9 KB

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