Selaa lähdekoodia

Made a setting to ban implicit conversion of Ascii to Unicode.

David Piuva 1 päivä sitten
vanhempi
sitoutus
35fff150d3

+ 7 - 9
Source/DFPSR/api/imageAPI.cpp

@@ -369,15 +369,6 @@ if (image_exists(FIRST) && image_exists(SECOND) && image_exists(THIRD) && image_
 }
 }
 OrderedImageRgbaU8 image_pack(const ImageU8& red, const ImageU8& green, const ImageU8& blue, const ImageU8& alpha) { PACK4(red, green, blue, alpha) }
 OrderedImageRgbaU8 image_pack(const ImageU8& red, const ImageU8& green, const ImageU8& blue, const ImageU8& alpha) { PACK4(red, green, blue, alpha) }
 
 
-// Convert a grayscale image into an ascii image using the given alphabet.
-//   Since all 256 characters cannot be in the alphabet, the encoding is lossy.
-// Each line is stored within <> to prevent text editors from removing meaningful white space.
-// The first line contains the given alphabet as a gradient from black to white.
-// Preconditions:
-//   alphabet may not have extended ascii, non printable, '\', '"', '>' or linebreak
-//   width <= stride
-//   size of monochromeImage = height * stride
-// Example alphabet: " .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 String image_toAscii(const ImageU8& image, const String& alphabet) {
 String image_toAscii(const ImageU8& image, const String& alphabet) {
 	if (!image_exists(image)) {
 	if (!image_exists(image)) {
 		return U"null";
 		return U"null";
@@ -509,6 +500,13 @@ AlignedImageU8 image_fromAscii(const String& content) {
 	return result;
 	return result;
 }
 }
 
 
+#ifdef BAN_IMPLICIT_ASCII_CONVERSION
+	AlignedImageU8 image_fromAscii(const char *content) {
+		// It is slightly redundant to allocate a temporary Unicode String, but saves instruction memory by only needing one implementation to handle both real Ascii and Ascii art embedded in Unicode.
+		return image_fromAscii(string_fromAscii(content));
+	}
+#endif
+
 template <typename IMAGE_TYPE, int32_t CHANNELS, typename ELEMENT_TYPE>
 template <typename IMAGE_TYPE, int32_t CHANNELS, typename ELEMENT_TYPE>
 ELEMENT_TYPE maxDifference_template(const IMAGE_TYPE& imageA, const IMAGE_TYPE& imageB) {
 ELEMENT_TYPE maxDifference_template(const IMAGE_TYPE& imageA, const IMAGE_TYPE& imageB) {
 	if (image_getWidth(imageA) != image_getWidth(imageB) || image_getHeight(imageA) != image_getHeight(imageB)) {
 	if (image_getWidth(imageA) != image_getWidth(imageB) || image_getHeight(imageA) != image_getHeight(imageB)) {

+ 15 - 0
Source/DFPSR/api/imageAPI.h

@@ -391,9 +391,24 @@ namespace dsr {
 	ImageRgbaU8 image_removePadding(const ImageRgbaU8& image);
 	ImageRgbaU8 image_removePadding(const ImageRgbaU8& image);
 
 
 // Ascii images
 // Ascii images
+	// Convert a grayscale image into an ascii image using the given alphabet.
+	//   Since all 256 characters cannot be in the alphabet, the encoding is lossy.
+	// Each line is stored within <> to prevent text editors from removing meaningful white space.
+	// The first line contains the given alphabet as a gradient from black to white.
+	// Preconditions:
+	//   alphabet may not have extended ascii, non printable, '\', '"', '>' or linebreak
+	//   width <= stride
+	//   size of monochromeImage = height * stride
+	// Example alphabet: " .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 	String image_toAscii(const ImageU8& image, const String &alphabet);
 	String image_toAscii(const ImageU8& image, const String &alphabet);
+	// A simplified version using a default alphabet.
 	String image_toAscii(const ImageU8& image);
 	String image_toAscii(const ImageU8& image);
+	// Generate an image from Ascii art embedded in text.
 	AlignedImageU8 image_fromAscii(const String &content);
 	AlignedImageU8 image_fromAscii(const String &content);
+	// When implicit conversion from const char* to String is not allowed according to settings.h, image_fromAscii will need an overload for accepting the type directly.
+	#ifdef BAN_IMPLICIT_ASCII_CONVERSION
+		AlignedImageU8 image_fromAscii(const char *content);
+	#endif
 
 
 // Comparisons
 // Comparisons
 	// Get the maximum pixelwise difference between two images of the same format, or the highest possible value on failure
 	// Get the maximum pixelwise difference between two images of the same format, or the highest possible value on failure

+ 9 - 1
Source/DFPSR/api/stringAPI.cpp

@@ -69,9 +69,17 @@ ReadableString::ReadableString(const DsrChar *content)
 : view(content, strlen_utf32(content)) {}
 : view(content, strlen_utf32(content)) {}
 
 
 String::String() {}
 String::String() {}
-String::String(const char* source) { atomic_append_ascii(*this, source); }
+#ifndef BAN_IMPLICIT_ASCII_CONVERSION
+	String::String(const char* source) { atomic_append_ascii(*this, source); }
+#endif
 String::String(const DsrChar* source) { atomic_append_utf32(*this, source); }
 String::String(const DsrChar* source) { atomic_append_utf32(*this, source); }
 
 
+String dsr::string_fromAscii(const char *text) {
+	String result;
+	atomic_append_ascii(result, text);
+	return result;
+}
+
 String& Printable::toStream(String& target) const {
 String& Printable::toStream(String& target) const {
 	return this->toStreamIndented(target, U"");
 	return this->toStreamIndented(target, U"");
 }
 }

+ 6 - 1
Source/DFPSR/api/stringAPI.h

@@ -30,6 +30,7 @@
 #include "../base/SafePointer.h"
 #include "../base/SafePointer.h"
 #include "../base/DsrTraits.h"
 #include "../base/DsrTraits.h"
 #include "../collection/List.h"
 #include "../collection/List.h"
+#include "../settings.h"
 
 
 // Define DSR_INTERNAL_ACCESS before any include to get internal access to exposed types
 // Define DSR_INTERNAL_ACCESS before any include to get internal access to exposed types
 #ifdef DSR_INTERNAL_ACCESS
 #ifdef DSR_INTERNAL_ACCESS
@@ -157,7 +158,9 @@ class String : public ReadableString {
 public:
 public:
 	// Constructors.
 	// Constructors.
 	String();
 	String();
-	String(const char* source);
+	#ifndef BAN_IMPLICIT_ASCII_CONVERSION
+		String(const char* source);
+	#endif
 	String(const DsrChar* source);
 	String(const DsrChar* source);
 	// Destructor.
 	// Destructor.
 	~String() {}
 	~String() {}
@@ -475,6 +478,8 @@ void string_fromDouble(String& target, double value, int decimalCount = 6, bool
 inline String string_fromDouble(double value, int decimalCount = 6, bool removeTrailingZeroes = true, DsrChar decimalCharacter = U'.', DsrChar negationCharacter = U'-') {
 inline String string_fromDouble(double value, int decimalCount = 6, bool removeTrailingZeroes = true, DsrChar decimalCharacter = U'.', DsrChar negationCharacter = U'-') {
 	String result; string_fromDouble(result, value, decimalCount, removeTrailingZeroes, decimalCharacter, negationCharacter); return result;
 	String result; string_fromDouble(result, value, decimalCount, removeTrailingZeroes, decimalCharacter, negationCharacter); return result;
 }
 }
+// When BAN_IMPLICIT_ASCII_CONVERSION is defined, this is the only constructor for creating a String from "" instead of U"".
+String string_fromAscii(const char *text);
 // Loading will try to find a byte order mark and can handle UTF-8 and UTF-16.
 // Loading will try to find a byte order mark and can handle UTF-8 and UTF-16.
 //   Failure to find a byte order mark will assume that the file's content is raw Latin-1,
 //   Failure to find a byte order mark will assume that the file's content is raw Latin-1,
 //   because automatic detection would cause random behaviour.
 //   because automatic detection would cause random behaviour.

+ 3 - 0
Source/DFPSR/settings.h

@@ -11,6 +11,9 @@
 		#define DSR_HARD_EXIT_ON_ERROR
 		#define DSR_HARD_EXIT_ON_ERROR
 	#endif
 	#endif
 
 
+	// Enable to ban any implicit string conversion from Ascii to Unicode, so that accidentally writing "" instead of U"" does not create temporary heap allocations.
+	//#define BAN_IMPLICIT_ASCII_CONVERSION
+
 	// If EXTRA_SAFE_POINTER_CHECKS is defined, debug mode will let SafePointer perform thread and allocation identity checks.
 	// If EXTRA_SAFE_POINTER_CHECKS is defined, debug mode will let SafePointer perform thread and allocation identity checks.
 	//     Makes sure that the accessed memory has not been freed, recycled or shared with the wrong thread.
 	//     Makes sure that the accessed memory has not been freed, recycled or shared with the wrong thread.
 	//     This will make memory access super slow but catch more memory errors when basic bound checks are not enough.
 	//     This will make memory access super slow but catch more memory errors when basic bound checks are not enough.