Microsoft.NET.StringTools Represents a string that can be converted to System.String with interning, i.e. by returning an existing string if it has been seen before and is still tracked in the intern table. Enumerator for the top-level struct. Enumerates characters of the string. The InternableString being enumerated. Index of the current span, -1 represents the inline span. Index of the current character in the current span, -1 if MoveNext has not been called yet. Returns the current character. Moves to the next character. True if there is another character, false if the enumerator reached the end. The span held by this struct, inline to be able to represent . May be empty. .NET Core does not keep a reference to the containing object in . In particular, it cannot recover the string if the span represents one. We have to hold the reference separately to be able to roundtrip String->InternableString->String without allocating a new String. Additional spans held by this struct. May be null. Constructs a new InternableString wrapping the given . The span to wrap. When wrapping a span representing an entire System.String, use Internable(string) for optimum performance. Constructs a new InternableString wrapping the given string. The string to wrap, must be non-null. Constructs a new InternableString wrapping the given SpanBasedStringBuilder. Gets the length of the string. Creates a new enumerator for enumerating characters in this string. Does not allocate. The enumerator. Returns true if the string is equal to another string by ordinal comparison. Another string. True if this string is equal to . Returns a System.String representing this string. Allocates memory unless this InternableString was created by wrapping a System.String in which case the original string is returned. The string. Returns true if this InternableString wraps a System.String and the same System.String is passed as the argument. The string to compare to. True is this instance wraps the given string. Converts this instance to a System.String while first searching for a match in the intern table. May allocate depending on whether the string has already been interned. Implements the simple yet very decently performing djb2-like hash function (xor version) as inspired by https://github.com/dotnet/runtime/blob/6262ae8e6a33abac569ab6086cdccc470c810ea4/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs#L810-L840 A stable hashcode of the string represented by this instance. Unlike the BCL method, this implementation works only on two characters at a time to cut on the complexity with characters that feed into the same operation but straddle multiple spans. Note that it must return the same value for a given string regardless of how it's split into spans (e.g. { "AB" } and { "A", "B" } have the same hash code). Hashes a memory block specified by a pointer and length. Pointer to the first character. Number of characters at . The running hash code. True if the incoming was calculated from an odd number of characters. The updated running hash code (not passed as a ref parameter to play nicely with JIT optimizations). Rotates an integer by the specified number of bits. The value to rotate. The number of bits. The rotated value. A StringBuilder replacement that keeps a list of spans making up the intermediate string rather than a copy of its characters. This has positive impact on both memory (no need to allocate space for the intermediate string) and time (no need to copy characters to the intermediate string). The method tries to intern the resulting string without even allocating it if it's already interned. Use to take advantage of pooling to eliminate allocation overhead of this class. Enumerator for the top-level class. Enumerates characters of the string. The spans being enumerated. Index of the current span. Index of the current character in the current span, -1 if MoveNext has not been called yet. Returns the current character. Moves to the next character. True if there is another character, false if the enumerator reached the end. Spans making up the rope. Internal getter to get the list of spans out of the SpanBasedStringBuilder. Constructs a new SpanBasedStringBuilder containing the given string. The string to wrap, must be non-null. Constructs a new empty SpanBasedStringBuilder with the given expected number of spans. Gets the length of the string. Gets the capacity of the SpanBasedStringBuilder in terms of number of spans it can hold without allocating. Creates a new enumerator for enumerating characters in this string. Does not allocate. The enumerator. Converts this instance to a System.String while first searching for a match in the intern table. May allocate depending on whether the string has already been interned. Releases this instance. Appends a string. The string to append. Appends a substring. The string to append. The start index of the substring within to append. The length of the substring to append. Appends a character span represented by . The character span to append. Removes leading white-space characters from the string. Removes trailing white-space characters from the string. Removes leading and trailing white-space characters from the string. Clears this instance making it represent an empty string. Per-thread instance of the SpanBasedStringBuilder, created lazily. This field serves as a per-thread one-item object pool, which is adequate for most use cases as the builder is not expected to be held for extended periods of time. Interns the given string, keeping only a weak reference to the returned value. The string to intern. A string equal to , could be the same object as . The intern pool does not retain strong references to the strings it's holding so strings are automatically evicted after they become unrooted. This is in contrast to System.String.Intern which holds strings forever. Interns the given readonly span of characters, keeping only a weak reference to the returned value. The character span to intern. A string equal to , could be the result of calling ToString() on . The intern pool does not retain strong references to the strings it's holding so strings are automatically evicted after they become unrooted. This is in contrast to System.String.Intern which holds strings forever. Returns a new or recycled . The SpanBasedStringBuilder. Call on the returned instance to recycle it. Enables diagnostics in the interner. Call to retrieve the diagnostic data. Retrieves the diagnostic data describing the current state of the interner. Make sure to call beforehand. Returns a instance back to the pool if possible. The instance to return. Implements the WeakStringCache functionality on modern .NET versions where ConcurrentDictionary is available. A cache of weak GC handles pointing to strings. Weak GC handles are functionally equivalent to WeakReference's but have less overhead (they're a struct as opposed to WR which is a finalizable class) at the expense of requiring manual lifetime management. As long as a string has an ordinary strong GC root elsewhere in the process and another string with the same hashcode hasn't reused the entry, the cache has a reference to it and can match it to an internable. When the string is collected, it is also automatically "removed" from the cache by becoming unrecoverable from the GC handle. GC handles that do not reference a live string anymore are freed lazily. Main entrypoint of this cache. Tries to look up a string that matches the given internable. If it succeeds, returns the string and sets cacheHit to true. If the string is not found, calls ExpensiveConvertToString on the internable, adds the resulting string to the cache, and returns it, setting cacheHit to false. The internable describing the string we're looking for. true if match found in cache, false otherwise. A string matching the given internable. Iterates over the cache and removes unused GC handles, i.e. handles that don't reference live strings. This is expensive so try to call such that the cost is amortized to O(1) per GetOrCreateEntry() invocation. Returns internal debug counters calculated based on the current state of the cache. Debug stats returned by GetDebugInfo(). Holds a weak GC handle to a string. Shared by all strings with the same hash code and referencing the last such string we've seen. Weak GC handle to the last string of the given hashcode we've seen. Returns true if the string referenced by the handle is still alive. Returns the string referenced by this handle if it is equal to the given internable. The internable describing the string we're looking for. The string matching the internable or null if the handle is referencing a collected string or the string is different. Sets the handle to the given string. If the handle is still referencing another live string, that string is effectively forgotten. The string to set. Frees the GC handle. Initial capacity of the underlying dictionary. The maximum size we let the collection grow before scavenging unused entries. Frees all GC handles and clears the cache. Returns internal debug counters calculated based on the current state of the cache. Implements interning based on a WeakStringCache. Enumerates the possible interning results. The cache to keep strings in. Number of times the regular interning path found the string in the cache. Number of times the regular interning path added the string to the cache. Total number of strings eliminated by interning. Total number of chars eliminated across all strings. Maps strings that went though the interning path to the number of times they have been seen. The higher the number the better the payoff of interning. Null if statistics gathering has not been enabled. Try to intern the string. The return value indicates the how the string was interned. WeakIntern the given InternableString. Returns a string with human-readable statistics. Releases all strings from the underlying intern table.