StringImpl.hx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package python.internal;
  2. import python.lib.Builtin;
  3. class StringImpl {
  4. public static function split (s:String, d:String) {
  5. return if (d == "") Builtin.list(s) else (s:Dynamic).split(d);
  6. }
  7. public static function charCodeAt(s:String, index:Int) {
  8. return if (s == null || s.length == 0 || index < 0 || index >= s.length) null else untyped ord(untyped __python_array_get__(s, index));
  9. }
  10. public static inline function charAt(s:String, index:Int) {
  11. return if (index < 0 || index >= s.length) "" else untyped __python_array_get__(s,index);
  12. }
  13. public static inline function lastIndexOf(s:String, str:String, ?startIndex:Int):Int {
  14. if (startIndex == null) {
  15. return (untyped s.rfind)(str, 0, s.length);
  16. } else {
  17. var i = (untyped s.rfind)(str, 0, startIndex+1);
  18. var startLeft = i == -1 ? Math.max(0,startIndex+1-str.length) : i+1;
  19. var check = (untyped s.find)(str, startLeft, s.length);
  20. if (check > i && check <= startIndex) {
  21. return check;
  22. } else {
  23. return i;
  24. }
  25. }
  26. }
  27. public static inline function fromCharCode( code : Int ) : String {
  28. var c = code;
  29. return untyped (''.join)(Builtin.map(Builtin.chr, [c]));
  30. }
  31. }