2
0
Эх сурвалжийг харах

[cpp] Add cpp.Lib stringReference/bytesReference. bytesReference can be rather unsafe, so use with caution.

Hugh 10 жил өмнө
parent
commit
27b5193876
1 өөрчлөгдсөн 18 нэмэгдсэн , 1 устгасан
  1. 18 1
      std/cpp/Lib.hx

+ 18 - 1
std/cpp/Lib.hx

@@ -82,7 +82,24 @@ class Lib {
 
 	public static function rethrow(inExp:Dynamic) { throw inExp; }
 
-	public static function stringReference(inExp:Dynamic) { throw inExp; }
+	public static function stringReference(inBytes:haxe.io.Bytes) : String
+   {
+      var result:String = "";
+      untyped __global__.__hxcpp_string_of_bytes(inBytes.b, result, 0, 0, true);
+      return result;
+   }
+
+	/**
+		Returns bytes referencing the content of a string.
+      Use with extreme caution - changing constant strings will crash.
+      Changing one string can cause others to change unexpectedly.
+      Only really safe if you are using it read-only or if it comes from stringReference above
+	**/
+	public inline static function bytesReference( s : String ) : haxe.io.Bytes {
+      var bytes = new haxe.io.BytesData();
+      untyped bytes.__unsafeStringReference(s);
+		return haxe.io.Bytes.ofData(bytes);
+	}
 
 	/**
 		Print the specified value on the default output.