• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fogfish / guid / 11651828057

03 Nov 2024 01:38PM UTC coverage: 97.732% (-2.0%) from 99.738%
11651828057

Pull #17

github

fogfish
update CI/CD
Pull Request #17: support base62 codec

51 of 60 new or added lines in 2 files covered. (85.0%)

431 of 441 relevant lines covered (97.73%)

1.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.0
/base62.go
1
/*
2

3
  Copyright 2012 Dmitry Kolesnikov, All Rights Reserved
4

5
  Licensed under the Apache License, Version 2.0 (the "License");
6
  you may not use this file except in compliance with the License.
7
  You may obtain a copy of the License at
8

9
      http://www.apache.org/licenses/LICENSE-2.0
10

11
  Unless required by applicable law or agreed to in writing, software
12
  distributed under the License is distributed on an "AS IS" BASIS,
13
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
  See the License for the specific language governing permissions and
15
  limitations under the License.
16

17
*/
18

19
package guid
20

21
import (
22
        "fmt"
23
        "math"
24
)
25

26
var (
27
        encoder = [62]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
28
        decoder = [256]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
29
)
30

31
func encode62(src []byte) []byte {
1✔
32
        if len(src) == 0 {
1✔
NEW
33
                return nil
×
NEW
34
        }
×
35

36
        rs := 0
1✔
37
        cs := int(math.Ceil(math.Log(256) / math.Log(62) * float64(len(src))))
1✔
38
        dst := make([]byte, cs)
1✔
39
        for i := range src {
2✔
40
                c := 0
1✔
41
                v := int(src[i])
1✔
42
                for j := cs - 1; j >= 0 && (v != 0 || c < rs); j-- {
2✔
43
                        v += 256 * int(dst[j])
1✔
44
                        dst[j] = byte(v % 62)
1✔
45
                        v /= 62
1✔
46
                        c++
1✔
47
                }
1✔
48
                rs = c
1✔
49
        }
50
        for i := range dst {
2✔
51
                dst[i] = encoder[dst[i]]
1✔
52
        }
1✔
53
        if cs > rs {
2✔
54
                return dst[cs-rs:]
1✔
55
        }
1✔
56
        return dst
1✔
57
}
58

59
func decode62(src []byte) ([]byte, error) {
1✔
60
        if len(src) == 0 {
1✔
NEW
61
                return nil, nil
×
NEW
62
        }
×
63

64
        rs := 0
1✔
65
        cs := int(math.Ceil(math.Log(62) / math.Log(256) * float64(len(src))))
1✔
66
        dst := make([]byte, cs)
1✔
67
        for i := range src {
2✔
68
                if src[i] == '\n' || src[i] == '\r' {
1✔
NEW
69
                        continue
×
70
                }
71
                c := 0
1✔
72
                v := int(decoder[src[i]])
1✔
73
                if v == 255 {
1✔
NEW
74
                        return nil, fmt.Errorf("corrupted input: %v", src[i])
×
NEW
75
                }
×
76
                for j := cs - 1; j >= 0 && (v != 0 || c < rs); j-- {
2✔
77
                        v += 62 * int(dst[j])
1✔
78
                        dst[j] = byte(v % 256)
1✔
79
                        v /= 256
1✔
80
                        c++
1✔
81
                }
1✔
82
                rs = c
1✔
83
        }
84
        if cs > rs {
2✔
85
                return dst[cs-rs:], nil
1✔
86
        }
1✔
87
        return dst, nil
1✔
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc