2022-10-15 13:48:45 +02:00
using System ;
2020-06-21 23:23:52 +02:00
using System.Collections.Generic ;
namespace MLEM.Extensions {
/// <summary>
/// A set of extensions for dealing with <see cref="char"/>
/// </summary>
2022-10-23 21:23:16 +02:00
[Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching, or CodePointSource.ToString for UTF-32 caching.")]
2020-06-21 23:23:52 +02:00
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>
2022-10-23 21:23:16 +02:00
[Obsolete("ToCachedString is deprecated. Consider using a more robust, custom implementation for text caching, or CodePointSource.ToString for UTF-32 caching.")]
2020-06-21 23:23:52 +02:00
public static string ToCachedString ( this char c ) {
2022-06-15 11:38:11 +02:00
if ( ! CharExtensions . Cache . TryGetValue ( c , out var ret ) ) {
2020-06-21 23:23:52 +02:00
ret = c . ToString ( ) ;
2022-06-15 11:38:11 +02:00
CharExtensions . Cache . Add ( c , ret ) ;
2020-06-21 23:23:52 +02:00
}
return ret ;
}
}
2022-06-17 18:23:47 +02:00
}