StringImpl.hx 2.0 KB

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