aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Tayx/Graphy - Ultimate Stats Monitor/Scripts/Util/G_Intstring.cs
blob: cb102feb1977b3098e74857bc9db3e1f1d4f4121 (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ---------------------------------------
 * Author:          Started by David Mkrtchyan, modified by Martin Pane (martintayx@gmail.com) (@tayx94)
 * Collaborators:   Lars Aalbertsen (@Rockylars)
 * Project:         Graphy - Ultimate Stats Monitor
 * Date:            18-May-18
 * Studio:          Tayx
 * 
 * This project is released under the MIT license.
 * Attribution is not required, but it is always welcomed!
 * -------------------------------------*/

using UnityEngine;

namespace Tayx.Graphy.Utils.NumString
{
    public static class G_IntString
    {
        /* ----- TODO: ----------------------------
         * Try and move the Init to a core method.
         * --------------------------------------*/

        #region Variables -> Private

        /// <summary>
        /// List of negative ints casted to strings.
        /// </summary>
        private static string[] negativeBuffer = new string[0];

        /// <summary>
        /// List of positive ints casted to strings.
        /// </summary>
        private static string[] positiveBuffer = new string[0];

        #endregion

        #region Properties -> Public

        /// <summary>
        /// Have the int buffers been initialized?
        /// </summary>
        public static bool Inited
        {
            get
            {
                return negativeBuffer.Length > 0 || positiveBuffer.Length > 0;
            }
        }

        /// <summary>
        /// The lowest int value of the existing number buffer.
        /// </summary>
        public static int MinValue
        {
            get
            {
                return -(negativeBuffer.Length - 1);
            }
        }

        /// <summary>
        /// The highest int value of the existing number buffer.
        /// </summary>
        public static int MaxValue
        {
            get
            {
                return positiveBuffer.Length - 1;
            }
        }

        #endregion

        #region Methods -> Public

        /// <summary>
        /// Initialize the buffers.
        /// </summary>
        /// <param name="minNegativeValue">
        /// Lowest negative value allowed.
        /// </param>
        /// <param name="maxPositiveValue">
        /// Highest positive value allowed.
        /// </param>
        public static void Init(int minNegativeValue, int maxPositiveValue)
        {
            if (minNegativeValue <= 0)
            {
                int length = Mathf.Abs(minNegativeValue);
                negativeBuffer = new string[length];
                for (int i = 0; i < length; i++)
                {
                    negativeBuffer[i] = (-i).ToString();
                }
            }
            if (maxPositiveValue >= 0)
            {
                positiveBuffer = new string[maxPositiveValue];
                for (int i = 0; i < maxPositiveValue; i++)
                {
                    positiveBuffer[i] = i.ToString();
                }
            }
        }
        
        /// <summary>
        /// Returns this int as a cached string.
        /// </summary>
        /// <param name="value">
        /// The required int.
        /// </param>
        /// <returns>
        /// A cached number string.
        /// </returns>
        public static string ToStringNonAlloc(this int value)
        {
            if (value < 0 && -value < negativeBuffer.Length)
            {
                return negativeBuffer[-value];
            }

            if (value >= 0 && value < positiveBuffer.Length)
            {
                return positiveBuffer[value];
            }

            return value.ToString();
        }

        #endregion
    }
}