|
|
@@ -61,7 +61,8 @@ enum class LineEncoding {
|
|
|
|
|
|
// 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.
|
|
|
-// Trying to assign String onto ReadableString by value will fail because String contains more members
|
|
|
+// 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.
|
|
|
class ReadableString {
|
|
|
IMPL_ACCESS:
|
|
|
// A local pointer to the sub-allocation
|
|
|
@@ -114,7 +115,7 @@ public:
|
|
|
// No combined characters allowed, use precomposed instead, so that the strings can guarantee a fixed character size
|
|
|
class String : public ReadableString {
|
|
|
IMPL_ACCESS:
|
|
|
- // A reference counted pointer to the buffer, just to keep the allocation
|
|
|
+ // 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
|
|
|
char32_t* writeSection = nullptr;
|
|
|
@@ -234,10 +235,25 @@ ReadableString string_from(const ReadableString& source, int64_t inclusiveStart)
|
|
|
// Example: string_after(U"0123456789", 5) == U"6789"
|
|
|
ReadableString string_after(const ReadableString& source, int64_t exclusiveStart);
|
|
|
|
|
|
-// TODO: Should string_split and string_split_inPlace be removed now that string_split_callback is both safer and faster?
|
|
|
-// This would remove the dependency on List, in case that one wants a different container.
|
|
|
-
|
|
|
-// Warning!
|
|
|
+// The safest split implementation
|
|
|
+// Post-condition:
|
|
|
+// Returns a list of strings from source by splitting along separator.
|
|
|
+// If removeWhiteSpace is true then surrounding white-space will be removed, otherwise white-space is kept.
|
|
|
+// 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.
|
|
|
+// 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);
|
|
|
+// The fastest and most powerful split implementation
|
|
|
+// 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.
|
|
|
+// Side-effects:
|
|
|
+// Calls action for each sub-string divided by separator in source.
|
|
|
+void string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
|
|
|
+// An alternative overload for having a very long lambda at the end.
|
|
|
+inline void string_split_callback(const ReadableString& source, DsrChar separator, bool removeWhiteSpace, std::function<void(ReadableString)> action) {
|
|
|
+ string_split_callback(action, source, separator, removeWhiteSpace);
|
|
|
+}
|
|
|
+// Warning! May cause a crash.
|
|
|
// 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.
|
|
|
// Use string_split_callback if you want something safer.
|
|
|
@@ -246,8 +262,8 @@ ReadableString string_after(const ReadableString& source, int64_t exclusiveStart
|
|
|
// 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.
|
|
|
// Each string in the list reuses memory from the input string using reference counting, but the list itself will be allocated.
|
|
|
-List<ReadableString> string_split(const ReadableString& source, DsrChar separator);
|
|
|
-// Warning!
|
|
|
+List<ReadableString> string_dangerous_split(const ReadableString& source, DsrChar separator);
|
|
|
+// Warning! May cause a crash.
|
|
|
// 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.
|
|
|
// Use string_split_callback if you want something safer.
|
|
|
@@ -257,13 +273,10 @@ List<ReadableString> string_split(const ReadableString& source, DsrChar separato
|
|
|
// Fills the target list with strings from source by splitting along separator.
|
|
|
// 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.
|
|
|
-void string_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult = false);
|
|
|
-// Split a string without needing a list to store the result.
|
|
|
-// This allow filtering and processing before saving the results,
|
|
|
-// but makes debugging more difficult and prevents pattern detection.
|
|
|
-// Side-effects:
|
|
|
-// Calls action for each sub-string divided by separator in source.
|
|
|
-void string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator);
|
|
|
+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.
|
|
|
+// Useful for pre-allocation.
|
|
|
+int64_t string_splitCount(const ReadableString& source, DsrChar separator);
|
|
|
|
|
|
// Post-condition: Returns true iff c is a digit.
|
|
|
// Digit <- '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
|