StringImpl.hx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package python.internal;
  2. import python.internal.Internal;
  3. import python.internal.HxBuiltin;
  4. @:keep
  5. @:native("HxString")
  6. class StringImpl {
  7. public static inline function split (s:String, d:String) {
  8. return if (d == "") Syntax.field(HxBuiltin, "list")(s) else Syntax.callField(s, "split", d);
  9. }
  10. public static function charCodeAt(s:String, index:Int) {
  11. return
  12. if (s == null || s.length == 0 || index < 0 || index >= s.length) null
  13. else Syntax.callField(HxBuiltin, "ord", Syntax.arrayAccess(s, index));
  14. }
  15. public static inline function charAt(s:String, index:Int) {
  16. return if (index < 0 || index >= s.length) "" else Syntax.arrayAccess(s,index);
  17. }
  18. public static inline function lastIndexOf(s:String, str:String, ?startIndex:Int):Int {
  19. if (startIndex == null) {
  20. return Syntax.callField(s, "rfind", str, 0, s.length);
  21. } else {
  22. var i = Syntax.callField(s, "rfind", str, 0, startIndex+1);
  23. var startLeft = i == -1 ? Syntax.callField(HxBuiltin, "max", 0,startIndex+1-str.length) : i+1;
  24. var check = Syntax.callField(s,"find", str, startLeft, s.length);
  25. if (check > i && check <= startIndex) {
  26. return check;
  27. } else {
  28. return i;
  29. }
  30. }
  31. }
  32. public static inline function toUpperCase (s:String) {
  33. return Syntax.callField(s, "upper");
  34. }
  35. public static inline function toLowerCase (s:String) {
  36. return Syntax.callField(s, "lower");
  37. }
  38. public static inline function indexOf (s:String, str:String, ?startIndex:Int) {
  39. if (startIndex == null)
  40. return Syntax.callField(s, "find", str);
  41. else
  42. return Syntax.callField(s, "find", str, startIndex);
  43. }
  44. public static inline function toString (s:String) {
  45. return s;
  46. }
  47. public static inline function get_length (s:String) {
  48. return Syntax.field(HxBuiltin, "len")(s);
  49. }
  50. public static inline function fromCharCode( code : Int ) : String {
  51. #if doc_gen
  52. return "";
  53. #else
  54. var c = code;
  55. return Syntax.callField('', "join",
  56. Syntax.callField(HxBuiltin, "map", Syntax.field(HxBuiltin, "chr"), [c])); // TODO: check cast
  57. #end
  58. }
  59. public static function substring( s:String, startIndex : Int, ?endIndex : Int ) : String {
  60. if (startIndex < 0) startIndex = 0;
  61. if (endIndex == null) {
  62. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  63. } else {
  64. if (endIndex < 0) endIndex = 0;
  65. if (endIndex < startIndex) {
  66. return Syntax.arrayAccess(s, endIndex, startIndex);
  67. } else {
  68. return Syntax.arrayAccess(s, startIndex, endIndex);
  69. }
  70. }
  71. }
  72. public static function substr( s:String, startIndex : Int, ?len : Int ) : String {
  73. if (len == null) {
  74. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  75. } else {
  76. if (len == 0) return "";
  77. return Syntax.arrayAccess(s, startIndex, startIndex+len);
  78. }
  79. }
  80. }