StringImpl.hx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 (s:Dynamic).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 inline function fromCharCode( code : Int ) : String {
  30. #if doc_gen
  31. return "";
  32. #else
  33. var c = code;
  34. return (untyped (''.join)(Builtin.map(Builtin.chr, cast [c])):String); // TODO: check cast
  35. #end
  36. }
  37. }