-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix bad usage of reflect package to convert unsafe! - checked by `go build -gcflags=-m` show all `req does not escape` - read more : golang/go#40701
- Loading branch information
1 parent
bd22e21
commit cc5c867
Showing
9 changed files
with
259 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* For license and copyright information please see LEGAL file in repository */ | ||
|
||
package convert | ||
|
||
import "../giti" | ||
|
||
const ( | ||
maxUint8Value uint8 = 255 | ||
maxUint16Value uint16 = 65535 | ||
maxUint32Value uint32 = 4294967295 | ||
) | ||
|
||
// Base10StringToUint8 Parse given string and returns uint8 | ||
func Base10StringToUint8(str string) (num uint8, err giti.Error) { | ||
if str == "" { | ||
return 0, ErrEmptyValue | ||
} | ||
|
||
var n uint16 | ||
for _, char := range []byte(str) { | ||
var numSegment byte | ||
// var char = strAsByteSlice[i] | ||
switch { | ||
case '0' <= char && char <= '9': | ||
numSegment = char - '0' | ||
default: | ||
return 0, ErrBadValue | ||
} | ||
n *= 10 | ||
n += uint16(numSegment) | ||
if n > uint16(maxUint8Value) { | ||
return 0, ErrBadValue | ||
} | ||
} | ||
return uint8(n), nil | ||
} | ||
|
||
// Base10StringToUint16 Parse given string and returns uint16 | ||
func Base10StringToUint16(str string) (num uint16, err giti.Error) { | ||
if str == "" { | ||
return 0, ErrEmptyValue | ||
} | ||
|
||
var n uint32 | ||
for _, char := range []byte(str) { | ||
var numSegment byte | ||
// var char = strAsByteSlice[i] | ||
switch { | ||
case '0' <= char && char <= '9': | ||
numSegment = char - '0' | ||
default: | ||
return 0, ErrBadValue | ||
} | ||
n *= 10 | ||
n += uint32(numSegment) | ||
if n > uint32(maxUint16Value) { | ||
return 0, ErrBadValue | ||
} | ||
} | ||
return uint16(n), nil | ||
} | ||
|
||
// Base10StringToUint32 Parse given string and returns uint32 | ||
func Base10StringToUint32(str string) (num uint32, err giti.Error) { | ||
if str == "" { | ||
return 0, ErrEmptyValue | ||
} | ||
|
||
var n uint64 | ||
for _, char := range []byte(str) { | ||
var numSegment byte | ||
// var char = strAsByteSlice[i] | ||
switch { | ||
case '0' <= char && char <= '9': | ||
numSegment = char - '0' | ||
default: | ||
return 0, ErrBadValue | ||
} | ||
n *= 10 | ||
n += uint64(numSegment) | ||
if n > uint64(maxUint32Value) { | ||
return 0, ErrBadValue | ||
} | ||
} | ||
return uint32(n), nil | ||
} | ||
|
||
// Base10StringToUint64 Parse given string and returns uint64 | ||
func Base10StringToUint64(str string) (num uint64, err giti.Error) { | ||
if str == "" { | ||
return 0, ErrEmptyValue | ||
} | ||
|
||
for _, char := range []byte(str) { | ||
var numSegment byte | ||
// var char = strAsByteSlice[i] | ||
switch { | ||
case '0' <= char && char <= '9': | ||
numSegment = char - '0' | ||
default: | ||
return 0, ErrBadValue | ||
} | ||
num *= 10 | ||
num += uint64(numSegment) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* For license and copyright information please see LEGAL file in repository */ | ||
|
||
package convert | ||
|
||
import ( | ||
er "../error" | ||
lang "../language" | ||
) | ||
|
||
const errorEnglishDomain = "Convert" | ||
const errorPersianDomain = "تبدیل" | ||
|
||
// Errors | ||
var ( | ||
ErrEmptyValue = er.New("urn:giti:convert.libgo:error:empty-value").SetDetail(lang.LanguageEnglish, errorEnglishDomain, "Empty Value", | ||
"Empty value pass to convert function that it is illegal", | ||
"", | ||
"").Save() | ||
|
||
ErrBadValue = er.New("urn:giti:convert.libgo:error:bad-value").SetDetail(lang.LanguageEnglish, errorEnglishDomain, "Bad Value", | ||
"Bad value pass to convert function that it is illegal e.g. pass '1b2' to convert to number", | ||
"", | ||
"").Save() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.