-
Notifications
You must be signed in to change notification settings - Fork 0
/
NAString.cs
111 lines (99 loc) · 4.92 KB
/
NAString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections.Generic;
using System.Globalization;
/// <summary>
/// <b>NAString</b> is a structure which is responsible for caching and representing a set of strings, such as float -> string conversion without constant allocations.
/// </summary>
public readonly struct NAString
{
#region Variables
/// <summary>
/// Dictionary which holds a decimal value as a key and a string as the display value.
/// </summary>
private static readonly Dictionary<decimal, string> decimalToString = new();
/// <summary>
/// Culture used for <c>*.ToString()</c> methods that ask for it.
/// </summary>
private static readonly CultureInfo culture = CultureInfo.InvariantCulture;
#endregion
/// <summary>
/// Flushes the entire cache.
/// </summary>
public static void FlushCache() => decimalToString.Clear();
/// <summary>
/// Adds a new string representation of a <see cref="float"/> value to the cache.
/// </summary>
/// <param name="value"></param>
/// <param name="toStringFormat"></param>
/// <returns><b>true</b> if added successfully, <b>false</b> if the same value is already present in the cache.</returns>
public static bool Allocate(float value, string toStringFormat = "")
{
var display = string.IsNullOrEmpty(toStringFormat)
? value.ToString(culture)
: value.ToString(toStringFormat);
return decimalToString.TryAdd((decimal)value, display);
}
/// <summary>
/// Adds a new string representation of a <see cref="int"/> value to the cache.
/// </summary>
/// <param name="value"></param>
/// <param name="toStringFormat"></param>
/// <returns><b>true</b> if added successfully, <b>false</b> if the same value is already present in the cache.</returns>
public static bool Allocate(int value, string toStringFormat = "")
{
var display = string.IsNullOrEmpty(toStringFormat)
? value.ToString(culture)
: value.ToString(toStringFormat);
return decimalToString.TryAdd(value, display);
}
/// <summary>
/// Adds a new string representation of a <see cref="double"/> value to the cache.
/// </summary>
/// <param name="value"></param>
/// <param name="toStringFormat"></param>
/// <returns><b>true</b> if added successfully, <b>false</b> if the same value is already present in the cache.</returns>
public static bool Allocate(double value, string toStringFormat = "")
{
var display = string.IsNullOrEmpty(toStringFormat)
? value.ToString(culture)
: value.ToString(toStringFormat);
return decimalToString.TryAdd((decimal)value, display);
}
/// <summary>
/// Tries to get a string representation of the specified <see cref="float"/> value from the cache.
/// </summary>
/// <param name="value"></param>
/// <returns>the <see cref="string"/> if found, otherwise <see cref="string.Empty"/>.</returns>
public static string Borrow(float value) =>
decimalToString.TryGetValue((decimal)value, out var f) ? f : string.Empty;
/// <summary>
/// Tries to get a string representation of the specified <see cref="int"/> value from the cache.
/// </summary>
/// <param name="value"></param>
/// <returns>the <see cref="string"/> if found, otherwise <see cref="string.Empty"/>.</returns>
public static string Borrow(int value) => decimalToString.TryGetValue(value, out var i) ? i : string.Empty;
/// <summary>
/// Tries to get a string representation of the specified <see cref="double"/> value from the cache.
/// </summary>
/// <param name="value"></param>
/// <returns>the <see cref="string"/> if found, otherwise <see cref="string.Empty"/>.</returns>
public static string Borrow(double value) =>
decimalToString.TryGetValue((decimal)value, out var d) ? d : string.Empty;
/// <summary>
/// Checks if the cache contains a string representation of the specified <see cref="float"/> value.
/// </summary>
/// <param name="value"></param>
/// <returns><b>true</b> if it was found in the cache, <b>false</b> if it weren't.</returns>
public static bool IsCached(float value) => decimalToString.ContainsKey((decimal)value);
/// <summary>
/// Checks if the cache contains a string representation of the specified <see cref="int"/> value.
/// </summary>
/// <param name="value"></param>
/// <returns><b>true</b> if it was found in the cache, <b>false</b> if it weren't.</returns>
public static bool IsCached(int value) => decimalToString.ContainsKey(value);
/// <summary>
/// Checks if the cache contains a string representation of the specified <see cref="double"/> value.
/// </summary>
/// <param name="value"></param>
/// <returns><b>true</b> if it was found in the cache, <b>false</b> if it weren't.</returns>
public static bool IsCached(double value) => decimalToString.ContainsKey((decimal)value);
}