StringImpl.hx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package python.internal;
  2. import python.internal.Internal;
  3. import python.lib.Builtin;
  4. @:keep
  5. @:native("HxString")
  6. class StringImpl {
  7. public static inline function split (s:String, d:String) {
  8. return if (d == "") Builtin.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 Builtin.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 ? Builtin.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 Builtin.len(s);
  49. }
  50. public static inline function fromCharCode( code : Int ) : String {
  51. #if doc_gen
  52. return "";
  53. #else
  54. return Syntax.callField('', "join", Builtin.map(Builtin.chr, cast [code])); // TODO: check cast
  55. #end
  56. }
  57. public static function substring( s:String, startIndex : Int, ?endIndex : Int ) : String {
  58. if (startIndex < 0) startIndex = 0;
  59. if (endIndex == null) {
  60. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  61. } else {
  62. if (endIndex < 0) endIndex = 0;
  63. if (endIndex < startIndex) {
  64. return Syntax.arrayAccess(s, endIndex, startIndex);
  65. } else {
  66. return Syntax.arrayAccess(s, startIndex, endIndex);
  67. }
  68. }
  69. }
  70. public static function substr( s:String, startIndex : Int, ?len : Int ) : String {
  71. if (len == null) {
  72. return Syntax.arrayAccessWithTrailingColon(s, startIndex);
  73. } else {
  74. if (len == 0) return "";
  75. return Syntax.arrayAccess(s, startIndex, startIndex+len);
  76. }
  77. }
  78. }