-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_IntShortening.ahk
160 lines (140 loc) · 4.22 KB
/
class_IntShortening.ahk
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
;SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
;SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
/**
* Class: IntShortening
* This class should implement a highly modifyable integer shortening into ahk
* Usage:
* Callable - NO
* Instantiable - YES
* Subclassable - YES
* Attributes:
* accuracy - integer
* number of fractional digits
* [1 - x] - strings
* the suffixes for each positiv Floor(Log(num)/3) step till x
* every value higher than x will use the the value of x as suffixe
* Methods:
* __New(accuracy, suffixes*)
* creates a new instance of the IntShortening class
* stdFloor(int)
* shortens the given integer using the standard format, floored to the instances accuracy
* stdRound(int)
* shortens the given integer using the standard format, rounded to the instances accuracy
* __Get(vKey)
* method to allow overflow protection
* Shortening Formats:
* std - standard format
* e.g. 123456 == 123.5k
*/
class IntShortening {
/**
* Method: __New(accuracy, suffixes*)
* creates a new IntShortening class object with its own suffixes and accuracy
* Params:
* accuracy - the number of fractional digits
* suffixes - The strings to be appended for each positiv Floor(Log(num)/3) step
* Return:
* IntShortening - the newly created class instance of the IntShortening class
* Throws:
* IllegalType - the handed accuracy is not of type integer
* Message - IllegalType
* What - Line of the calling function
* Extra - the handed value for accuracy
*/
__New(accuracy, suffixes*){
if accuracy is not Integer
Throw, Exception("Illegal type", -1, accuracy)
this.accuracy := Abs(accuracy)
for k, i in suffixes
this[k] := i
}
/**
* Method: stdFloor(int)
* shortens the given integer using the standard format, floored to the instances accuracy
* Params:
* int - the integer to be shortened
* Return:
* shortened int - the shortened version of the handed integer
* Throws:
* IllegalType - the handed integer is not of type integer
* Message - IllegalType
* What - Line of the calling function
* Extra - the handed value for int
*/
stdFloor(int){
if int is not Integer
Throw, Exception("Illegal type", -1, int)
if(int < 1000)
return, Format("{1:d}{2:s}", int, (this.0) ? this.0 : "")
size := Floor(Floor(Log(int)) / 3)
While(size > 0){
Try {
ending := this[size]
break
}
Catch e {
size--
}
}
return, Round(Floor(int / (10 ** (size * 3 - this.accuracy))) / (10 ** this.accuracy), this.accuracy) . ending
}
/**
* Method: stdRound(int)
* shortens the given integer using the standard format, rounded to the instances accuracy
* Params:
* int - the integer to be shortened
* Return:
* shortened int - the shortened version of the handed integer
* Throws:
* IllegalType - the handed accuracy is not of type integer
* Message - IllegalType
* What - Line of the calling function
* Extra - the handed value for accuracy
*/
stdRound(int){
if int is not Integer
Throw, Exception("Illegal type", -1, int)
if(int < 1000)
return, Format("{1:d}{2:s}", int, (this.0) ? this.0 : "")
size := Floor(Floor(Log(int)) / 3)
While(size > 0){
Try {
ending := this[size]
break
}
Catch e {
size--
}
}
return, Round(int / (10 ** (size * 3)), this.accuracy) . ending
}
/**
* Method: __Get(vKey)
* this method handles the retrieval of keys not set before
* it plays a crucial part in the overflow protection system
* Do not modify this method unless you really know what you're doing
* Params:
* vKey - the undefined key that should be evaluated
* Return:
* value for prefined keys
* Throws:
* IntKeyNonexistent - the handed integer key does not exist
* Message - IntKeyNonexistent
* What - Line of the calling function
* Extra - the handed key
*/
__Get(vKey){
if(vKey == "accuracy")
return, 1
if vKey is Integer
{
if(vKey)
Throw, Exception("IntKeyNonexistent", -1, vKey)
else
return, 0
} else
return, ""
}
}