@@ -6,58 +6,80 @@ import (
66)
77
88const (
9- Letter = 'l'
10- LetterDigit = 'L'
9+ LetterDigit = '#'
1110 Digit = '0'
11+
12+ LowerLetterDigit = 'L'
13+ LowerLetter = 'l'
14+
15+ UpperLetterDigit = 'U'
16+ UpperLetter = 'u'
1217)
1318
1419var ErrValue = errors .New ("invalid value" )
1520
1621func Apply (mask , value string ) (string , error ) {
17- var err error
18- runes := make ([]rune , len (mask ))
22+ valueRunes := [] rune ( value )
23+ runes := make ([]rune , 0 , len (mask ))
1924 cursor := 0
20- for i , mT := range mask {
21- runes [i ], err = getRune (mT , value , & cursor )
22- if err != nil {
23- return "" , err
25+
26+ var r rune
27+ for _ , mT := range mask {
28+ if isMaskType (mT ) {
29+ r = getRune (mT , valueRunes , & cursor )
30+ if r == - 1 {
31+ return string (runes ), ErrValue
32+ }
33+ } else {
34+ r = mT
2435 }
36+ runes = append (runes , r )
2537 }
2638
27- str := string (runes )
28- return str , nil
39+ return string (runes ), nil
2940}
3041
31- func getRune (maskType rune , value string , cursor * int ) ( rune , error ) {
42+ func getRune (maskType rune , value [] rune , cursor * int ) rune {
3243 if (* cursor ) > len (value )- 1 {
33- return 0 , ErrValue
44+ return - 1
3445 }
3546
36- r := rune (value [* cursor ])
37-
38- if maskType == r {
39- * cursor ++
40- return maskType , nil
41- } else if isMaskType (maskType ) {
42- * cursor ++
43- if (maskType == LetterDigit && isLetterDigit (r )) ||
44- (maskType == Letter && unicode .IsLetter (r )) ||
45- (maskType == Digit && unicode .IsDigit (r )) {
46- return r , nil
47- }
48- return getRune (maskType , value , cursor )
49- } else {
50- return maskType , nil
47+ r := value [* cursor ]
48+ * cursor ++
49+
50+ // # - Requires a unicode letter or digit at this position
51+ // 0 - Requires a digit at this position
52+ if (maskType == LetterDigit && isLetterOrDigit (r )) || (maskType == Digit && unicode .IsDigit (r )) {
53+ return r
5154 }
55+
56+ // L - Requires a unicode letter (will be lower) or digit at this position
57+ // l - Requires a unicode letter (will be lower) at this position
58+ if (maskType == LowerLetterDigit && isLetterOrDigit (r )) || (maskType == LowerLetter && unicode .IsLetter (r )) {
59+ return unicode .ToLower (r )
60+ }
61+
62+ // U - Requires a unicode letter (will be upper) or digit at this position
63+ // u - Requires a unicode letter (will be upper) at this position
64+ if (maskType == UpperLetterDigit && isLetterOrDigit (r )) || (maskType == UpperLetter && unicode .IsLetter (r )) {
65+ return unicode .ToUpper (r )
66+ }
67+
68+ return getRune (maskType , value , cursor )
5269}
5370
54- func isLetterDigit (r rune ) bool {
71+ func isLetterOrDigit (r rune ) bool {
5572 return unicode .IsLetter (r ) || unicode .IsDigit (r )
5673}
5774
5875func isMaskType (r rune ) bool {
5976 switch r {
60- case LetterDigit , Letter , Digit :
77+ case LetterDigit ,
78+ Digit ,
79+ LowerLetterDigit ,
80+ LowerLetter ,
81+ UpperLetterDigit ,
82+ UpperLetter :
6183 return true
6284 default :
6385 return false
0 commit comments