This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move filter.fastParseInt to convert.ToInt
This has been extracted so that it can be used outside of the filter package.
- Loading branch information
Showing
5 changed files
with
104 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2018 Jump Trading | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package convert | ||
|
||
import "errors" | ||
|
||
const int64Max = (1 << 63) - 1 | ||
|
||
// ToInt is a simpler, faster version of strconv.ParseInt(). | ||
// Differences to ParseInt: | ||
// - input is []byte instead of a string (no type conversion required | ||
// by caller) | ||
// - only supports base 10 input | ||
// - only handles positive values | ||
func ToInt(s []byte) (int64, error) { | ||
if len(s) == 0 { | ||
return 0, errors.New("empty") | ||
} | ||
|
||
var n uint64 | ||
for _, c := range s { | ||
if '0' <= c && c <= '9' { | ||
c -= '0' | ||
} else { | ||
return 0, errors.New("invalid char") | ||
} | ||
n = n*10 + uint64(c) | ||
} | ||
|
||
if n > int64Max { | ||
return 0, errors.New("overflow") | ||
} | ||
return int64(n), nil | ||
} |
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,51 @@ | ||
// Copyright 2018 Jump Trading | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build small | ||
|
||
package convert_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/jumptrading/influx-spout/convert" | ||
) | ||
|
||
func TestToInt(t *testing.T) { | ||
check := func(input string, expected int64) { | ||
actual, err := convert.ToInt([]byte(input)) | ||
require.NoError(t, err) | ||
assert.Equal(t, expected, actual, "ToInt(%q)", input) | ||
} | ||
|
||
shouldFail := func(input string) { | ||
_, err := convert.ToInt([]byte(input)) | ||
assert.Error(t, err) | ||
} | ||
|
||
check("0", 0) | ||
check("1", 1) | ||
check("9", 9) | ||
check("10", 10) | ||
check("99", 99) | ||
check("101", 101) | ||
check("9223372036854775807", (1<<63)-1) // max int64 value | ||
|
||
shouldFail("9223372036854775808") // max int64 value + 1 | ||
shouldFail("-1") // negatives not supported | ||
shouldFail("x") | ||
} |
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