-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_CharsetTranslator.ahk
149 lines (129 loc) · 9.23 KB
/
class_CharsetTranslator.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
/**
* Class: CharsetTranslator
* Classbased framework for any charset conversion
* Attributes:
* __init__: true after the class has been initialised
* dict: An instance of COMDict.ahk as dictionary for conversions
* byChar: True in case the replacement should work char by char
* Methods:
* __New(dict)
* creates a new CharsetTranslator instance
* encode(sequence)
* encodes the given sequence using its classes dict
* decode(sequence)
* decodes the given sequence using its classes dict inverted
* _translator(sequence, dict, byChar := "")
* decides which translater method to use
* _translater(sequence, dict)
* translates using RegExReplace to replace
* _translaterByChar(sequence, dict)
* translates by evaluating every char individually
* _initialise()
* Implement this method along with a static __init__
* Dependencys:
* COMDict.ahk: https://github.com/Andreas-Coding/COMDict.ahk
*/
Class CharsetTranslator{
/**
* Static Attribute: __init__
* used for automatic initialisation of
* derived static classes. To be used with a
* a customised _initialise() method.
* Note:
* __init__ should be set true to mark itself as initialised
*/
;static __init__ := DerivedClassName._initialise()
/**
* Method: __New(dict)
* creates a new CharsetTranslator instance
* with the handed dict as dictionary
* Params:
* dict: An instance of the COMDict class
* to be used for translation
* Return:
* the newly created instance or false in case
* case the dict isn't an COMDict instance
*/
__New(dict){
if(!COMDict.isCOMDict(dict))
return, ""
this.dict := dict
}
/**
* Method: encode(sequence)
* encodes the given sequence using its classes dict
* Params:
* sequence: the sequence to be encoded
* Return:
* the encoded sequence
*/
encode(sequence){
return, this._translator(sequence, this.dict, this.byChar)
}
/**
* Method: decode(sequence)
* decodes the given sequence using its classes dict inverted
* Params:
* sequence: the sequence to be decoded
* Return:
* the decoded sequence
*/
decode(sequence){
return, this._translator(sequence, this.dict.invert(), this.byChar)
}
/**
* Method: _translator(sequence, dict, byChar := "")
* decides which translater method to use
* Params:
* sequence: the sequence to be de- / encoded
* dict: the dict to be use for the translation
* byChar: whether to use byChar method
* Return:
* the de- / encoded sequence
*/
_translator(sequence, dict, byChar := ""){
return, this["_translator" . ((byChar) ? "ByChar" : "")](sequence, dict)
}
/**
* Method: _translater(sequence, dict)
* translates using RegExReplace to replace
* Params:
* sequence: the sequence to be de- / encoded
* dict: the dict to use for the translation
* Return:
* the de- / encoded sequence
*/
_translater(sequence, dict){
for k, v in dict
sequence := RegExReplace(sequence, k, v)
return, sequence
}
/**
* Method: _translaterByChar(sequence, dict)
* translates by evaluating every char individually
* Params:
* sequence: the sequence to be de- / encoded
* dict: the dict to use for the translation
* Return:
* the de- / encoded sequence
*/
_translaterByChar(sequence, dict){
for _, v in StrSplit(sequence){
r .= ((dict.exists(v)) ? (dict.item(v)) : v)
}
return, r
}
/**
* Method: _initialise()
* Implement this method along with a
* static __init__ := DerivedClassName._initialise()
* within the class itself to build a static translator
* Params: As you like/need
* Return:
* True to signalise that the static class has been initialised
*/
_initialise(){
;your code to create a dict for a static derivate of CharsetTranslator Class
}
}
#Include, %A_LineFile%/../lib/COMDict/COMDict.ahk