|
@@ -59,12 +59,18 @@ enum class LineEncoding {
|
|
|
Lf // Linux and Macintosh compatible (Might not work on non-portable text editors on Microsoft Windows)
|
|
Lf // Linux and Macintosh compatible (Might not work on non-portable text editors on Microsoft Windows)
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+class String;
|
|
|
|
|
+
|
|
|
// Replacing String with a ReadableString reference for input arguments can make passing of U"" literals faster.
|
|
// Replacing String with a ReadableString reference for input arguments can make passing of U"" literals faster.
|
|
|
-// Unlike String, it cannot be constructed from a "" literal, because UTF-32 is used internally.
|
|
|
|
|
|
|
+// Unlike String, it cannot be constructed from a "" literal,
|
|
|
|
|
+// because it's not allowed to create a new allocation for the UTF-32 conversion.
|
|
|
// Only use by reference for input arguments or to hold temporary results!
|
|
// Only use by reference for input arguments or to hold temporary results!
|
|
|
// A ReadableString created as a sub-string from String will stop working once the parent String is freed.
|
|
// A ReadableString created as a sub-string from String will stop working once the parent String is freed.
|
|
|
class ReadableString {
|
|
class ReadableString {
|
|
|
IMPL_ACCESS:
|
|
IMPL_ACCESS:
|
|
|
|
|
+ // A reference counted pointer to the buffer to allow passing strings around without having to clone the buffer each time
|
|
|
|
|
+ // ReadableString only uses it for reference counting but String use it for reallocating
|
|
|
|
|
+ Buffer buffer;
|
|
|
// A local pointer to the sub-allocation
|
|
// A local pointer to the sub-allocation
|
|
|
const char32_t* readSection = nullptr;
|
|
const char32_t* readSection = nullptr;
|
|
|
// The length of the current string in characters
|
|
// The length of the current string in characters
|
|
@@ -81,6 +87,8 @@ public:
|
|
|
// Do not use ReadableString for heap allocated allocations that might be freed during the string's life!
|
|
// Do not use ReadableString for heap allocated allocations that might be freed during the string's life!
|
|
|
// String can handle dynamic memory and should be used in that case.
|
|
// String can handle dynamic memory and should be used in that case.
|
|
|
ReadableString(const DsrChar *content);
|
|
ReadableString(const DsrChar *content);
|
|
|
|
|
+ // Create from String while keeping check of reference counting
|
|
|
|
|
+ ReadableString(const String& source);
|
|
|
// Destructor
|
|
// Destructor
|
|
|
virtual ~ReadableString();
|
|
virtual ~ReadableString();
|
|
|
public:
|
|
public:
|
|
@@ -90,8 +98,6 @@ public:
|
|
|
virtual std::string toStdString() const;
|
|
virtual std::string toStdString() const;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-class String;
|
|
|
|
|
-
|
|
|
|
|
// Used as format tags around numbers passed to string_append or string_combine
|
|
// Used as format tags around numbers passed to string_append or string_combine
|
|
|
// New types can implement printing to String by making wrappers from this class
|
|
// New types can implement printing to String by making wrappers from this class
|
|
|
class Printable {
|
|
class Printable {
|
|
@@ -115,9 +121,7 @@ public:
|
|
|
// No combined characters allowed, use precomposed instead, so that the strings can guarantee a fixed character size
|
|
// No combined characters allowed, use precomposed instead, so that the strings can guarantee a fixed character size
|
|
|
class String : public ReadableString {
|
|
class String : public ReadableString {
|
|
|
IMPL_ACCESS:
|
|
IMPL_ACCESS:
|
|
|
- // A reference counted pointer to the buffer to allow passing strings around without having to clone the buffer each time
|
|
|
|
|
- Buffer buffer;
|
|
|
|
|
- // Same as readSection, but with write access
|
|
|
|
|
|
|
+ // Same as readSection, but with write access for appending more text
|
|
|
char32_t* writeSection = nullptr;
|
|
char32_t* writeSection = nullptr;
|
|
|
// Internal constructor
|
|
// Internal constructor
|
|
|
String(Buffer buffer, DsrChar *content, int64_t length);
|
|
String(Buffer buffer, DsrChar *content, int64_t length);
|
|
@@ -149,10 +153,6 @@ public:
|
|
|
void append(const std::string& source);
|
|
void append(const std::string& source);
|
|
|
// Extend the String using another character
|
|
// Extend the String using another character
|
|
|
void appendChar(DsrChar source);
|
|
void appendChar(DsrChar source);
|
|
|
-public:
|
|
|
|
|
- // Access
|
|
|
|
|
- void write(int64_t index, DsrChar value);
|
|
|
|
|
- void clear();
|
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// Define this overload for non-virtual source types that cannot inherit from Printable
|
|
// Define this overload for non-virtual source types that cannot inherit from Printable
|
|
@@ -246,7 +246,7 @@ ReadableString string_after(const ReadableString& source, int64_t exclusiveStart
|
|
|
// The separating characters are excluded from the resulting strings.
|
|
// The separating characters are excluded from the resulting strings.
|
|
|
// The number of strings returned in the list will equal the number of separating characters plus one, so the result may contain empty strings.
|
|
// The number of strings returned in the list will equal the number of separating characters plus one, so the result may contain empty strings.
|
|
|
// Each string in the list clones content to its own dynamic buffer. Use string_split_callback if you don't need long term storage.
|
|
// Each string in the list clones content to its own dynamic buffer. Use string_split_callback if you don't need long term storage.
|
|
|
-List<String> string_split_clone(const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
|
|
|
|
|
|
|
+List<String> string_split(const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
|
|
|
// The fastest and most powerful split implementation
|
|
// The fastest and most powerful split implementation
|
|
|
// Split a string without needing a list to store the result.
|
|
// Split a string without needing a list to store the result.
|
|
|
// Call it twice using different lambda functions if you want to count the size before allocating a buffer.
|
|
// Call it twice using different lambda functions if you want to count the size before allocating a buffer.
|
|
@@ -257,6 +257,10 @@ void string_split_callback(std::function<void(ReadableString)> action, const Rea
|
|
|
inline void string_split_callback(const ReadableString& source, DsrChar separator, bool removeWhiteSpace, std::function<void(ReadableString)> action) {
|
|
inline void string_split_callback(const ReadableString& source, DsrChar separator, bool removeWhiteSpace, std::function<void(ReadableString)> action) {
|
|
|
string_split_callback(action, source, separator, removeWhiteSpace);
|
|
string_split_callback(action, source, separator, removeWhiteSpace);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// TODO: ReadableString is no longer supposed to be unsafe, but String is also meant to be faster.
|
|
|
|
|
+
|
|
|
// Warning! May cause a crash.
|
|
// Warning! May cause a crash.
|
|
|
// Do not use a ReadableString generated by splitting a String past the String's lifetime.
|
|
// Do not use a ReadableString generated by splitting a String past the String's lifetime.
|
|
|
// ReadableString does not allocate any heap memory but is only a view for data allocated elsewhere.
|
|
// ReadableString does not allocate any heap memory but is only a view for data allocated elsewhere.
|
|
@@ -278,6 +282,8 @@ List<ReadableString> string_dangerous_split(const ReadableString& source, DsrCha
|
|
|
// If appendResult is false (default), any pre-existing elements in the target list will be cleared before writing the result.
|
|
// If appendResult is false (default), any pre-existing elements in the target list will be cleared before writing the result.
|
|
|
// If appendResult is true, the result is appended to the existing target list.
|
|
// If appendResult is true, the result is appended to the existing target list.
|
|
|
void string_dangerous_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult = false);
|
|
void string_dangerous_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult = false);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
// Split source using separator, only to return the number of splits.
|
|
// Split source using separator, only to return the number of splits.
|
|
|
// Useful for pre-allocation.
|
|
// Useful for pre-allocation.
|
|
|
int64_t string_splitCount(const ReadableString& source, DsrChar separator);
|
|
int64_t string_splitCount(const ReadableString& source, DsrChar separator);
|
|
@@ -368,7 +374,7 @@ String string_mangleQuote(const ReadableString &rawText);
|
|
|
String string_unmangleQuote(const ReadableString& mangledText);
|
|
String string_unmangleQuote(const ReadableString& mangledText);
|
|
|
|
|
|
|
|
// Post-condition: Returns the number of strings using the same buffer, including itself.
|
|
// Post-condition: Returns the number of strings using the same buffer, including itself.
|
|
|
-int64_t string_getBufferUseCount(const String& text);
|
|
|
|
|
|
|
+int64_t string_getBufferUseCount(const ReadableString& text);
|
|
|
|
|
|
|
|
// Ensures safely that at least minimumLength characters can he held in the buffer
|
|
// Ensures safely that at least minimumLength characters can he held in the buffer
|
|
|
inline void string_reserve(String& target, int64_t minimumLength) {
|
|
inline void string_reserve(String& target, int64_t minimumLength) {
|