BytesData.hx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package php;
  2. private class Wrapper {
  3. public var s : NativeString;
  4. public inline function new (s:NativeString) {
  5. this.s = s;
  6. }
  7. }
  8. abstract BytesData(Wrapper) {
  9. inline function new (x:Wrapper) this = x;
  10. inline function str ():php.NativeString return this.s;
  11. inline function setNativeString (val:NativeString):Void {
  12. this.s = val;
  13. }
  14. inline function get_length ():Int {
  15. return untyped __call__("strlen", str());
  16. }
  17. static inline function wrap (s:NativeString):Wrapper {
  18. return new Wrapper(s);
  19. }
  20. static inline function ofNativeString (s:NativeString) {
  21. return new BytesData( wrap(s));
  22. }
  23. public inline function set (index:Int, val:Int):Void {
  24. untyped __php__("{0}->s[{1}] = chr({2})", this, index, val);
  25. }
  26. public var length(get, never):Int;
  27. public inline function compare (other:BytesData):Int {
  28. return untyped __php__("{0} < {1} ? -1 : ({0} == {1} ? 0 : 1)", str(), other.str());
  29. }
  30. public inline function get (pos:Int):Int {
  31. return untyped __call__("ord", str()[pos]);
  32. }
  33. public inline function copy ():BytesData {
  34. return ofNativeString(str());
  35. }
  36. public inline function getString (pos:Int, len:Int):String {
  37. return untyped __call__("substr", str(), pos, len);
  38. }
  39. public inline function sub (pos:Int, len:Int):BytesData {
  40. return ofString(untyped __call__("substr", str(), pos, len));
  41. }
  42. public inline function blit (pos : Int, src : BytesData, srcpos : Int, len : Int):Void {
  43. setNativeString(untyped __php__("substr({0}, 0, {2}) . substr({1}, {3}, {4}) . substr({0}, {2}+{4})", str(), src.str(), pos, srcpos, len));
  44. }
  45. public inline function toString():String return cast str();
  46. public static inline function ofString (s:String) {
  47. return ofNativeString(cast s);
  48. }
  49. public static inline function alloc (length:Int) {
  50. return ofNativeString(untyped __call__("str_repeat", __call__("chr", 0), length));
  51. }
  52. }