forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.go
108 lines (94 loc) · 2.63 KB
/
hex.go
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
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package mysql
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/juju/errors"
)
// Hex is for mysql hexadecimal literal type.
type Hex struct {
// Value holds numeric value for hexadecimal literal.
Value int64
}
// String implements fmt.Stringer interface.
func (h Hex) String() string {
s := fmt.Sprintf("%X", h.Value)
if len(s)%2 != 0 {
return "0x0" + s
}
return "0x" + s
}
// ToNumber changes hexadecimal type to float64 for numeric operation.
// MySQL treats hexadecimal literal as double type.
func (h Hex) ToNumber() float64 {
return float64(h.Value)
}
// ToString returns the string representation for hexadecimal literal.
func (h Hex) ToString() string {
s := fmt.Sprintf("%x", h.Value)
if len(s)%2 != 0 {
s = "0" + s
}
// should never error.
b, _ := hex.DecodeString(s)
return string(b)
}
func uniformHexStrLit(s string) (string, error) {
if len(s) == 0 {
return "", errors.Errorf("invalid empty string for parsing hexadecimal literal")
}
if s[0] == 'x' || s[0] == 'X' {
// format is x'val' or X'val'
s = strings.Trim(s[1:], "'")
if len(s)%2 != 0 {
return "", errors.Errorf("invalid hexadecimal format, must even numbers, but %d", len(s))
}
s = "0x" + s
} else if !strings.HasPrefix(s, "0x") {
// here means format is not x'val', X'val' or 0xval.
return "", errors.Errorf("invalid hexadecimal format %s", s)
}
return s, nil
}
// ParseHex parses hexadecimal literal string.
// The string format can be X'val', x'val' or 0xval.
// val must in (0...9, a...f, A...F).
func ParseHex(s string) (Hex, error) {
var err error
s, err = uniformHexStrLit(s)
if err != nil {
return Hex{}, errors.Trace(err)
}
n, err := strconv.ParseInt(s, 0, 64)
if err != nil {
return Hex{}, errors.Trace(err)
}
return Hex{Value: n}, nil
}
// ParseHexStr parses hexadecimal literal as string.
// See https://dev.mysql.com/doc/refman/5.7/en/hexadecimal-literals.html
func ParseHexStr(s string) (string, error) {
var err error
s, err = uniformHexStrLit(s)
if err != nil {
return "", errors.Trace(err)
}
bs, err := hex.DecodeString(s[2:])
if err != nil {
return "", errors.Trace(err)
}
return string(bs), nil
}