BytesData.hx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package php;
  23. private class Wrapper {
  24. public var s : NativeString;
  25. public inline function new (s:NativeString) {
  26. this.s = s;
  27. }
  28. }
  29. abstract BytesData(Wrapper) {
  30. inline function new (x:Wrapper) this = x;
  31. inline function str ():php.NativeString return this.s;
  32. inline function setNativeString (val:NativeString):Void {
  33. this.s = val;
  34. }
  35. inline function get_length ():Int {
  36. return untyped __call__("strlen", str());
  37. }
  38. static inline function wrap (s:NativeString):Wrapper {
  39. return new Wrapper(s);
  40. }
  41. static inline function ofNativeString (s:NativeString) {
  42. return new BytesData( wrap(s));
  43. }
  44. public inline function set (index:Int, val:Int):Void {
  45. untyped __php__("{0}->s[{1}] = chr({2})", this, index, val);
  46. }
  47. public var length(get, never):Int;
  48. public inline function compare (other:BytesData):Int {
  49. return untyped __php__("{0} < {1} ? -1 : ({0} == {1} ? 0 : 1)", str(), other.str());
  50. }
  51. public inline function get (pos:Int):Int {
  52. return untyped __call__("ord", str()[pos]);
  53. }
  54. public inline function copy ():BytesData {
  55. return ofNativeString(str());
  56. }
  57. public inline function getString (pos:Int, len:Int):String {
  58. return untyped __call__("substr", str(), pos, len);
  59. }
  60. public inline function sub (pos:Int, len:Int):BytesData {
  61. return ofString(untyped __call__("substr", str(), pos, len));
  62. }
  63. public inline function blit (pos : Int, src : BytesData, srcpos : Int, len : Int):Void {
  64. setNativeString(untyped __php__("substr({0}, 0, {2}) . substr({1}, {3}, {4}) . substr({0}, {2}+{4})", str(), src.str(), pos, srcpos, len));
  65. }
  66. public inline function toString():String return cast str();
  67. public static inline function ofString (s:String) {
  68. return ofNativeString(cast s);
  69. }
  70. public static inline function alloc (length:Int) {
  71. return ofNativeString(untyped __call__("str_repeat", __call__("chr", 0), length));
  72. }
  73. }