mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-07-01 00:16:36 +02:00
30 lines
1.3 KiB
C#
30 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace MLEM.Extensions {
|
|
/// <summary>
|
|
/// A set of extensions for dealing with <see cref="char"/>
|
|
/// </summary>
|
|
[Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching, or CodePointSource.ToString for UTF-32 caching.")]
|
|
public static class CharExtensions {
|
|
|
|
private static readonly Dictionary<char, string> Cache = new Dictionary<char, string>();
|
|
|
|
/// <summary>
|
|
/// Returns the string representation of this character which will be stored and retrieved from a dictionary cache.
|
|
/// This method reduces string allocations, making it trade in processor efficiency for memory efficiency.
|
|
/// </summary>
|
|
/// <param name="c">The character to turn into a string</param>
|
|
/// <returns>A string representing the character</returns>
|
|
[Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching, or CodePointSource.ToString for UTF-32 caching.")]
|
|
public static string ToCachedString(this char c) {
|
|
if (!CharExtensions.Cache.TryGetValue(c, out var ret)) {
|
|
ret = c.ToString();
|
|
CharExtensions.Cache.Add(c, ret);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
}
|
|
}
|