-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for counting bytes in strings.
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 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,31 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <rparman@northwood-labs.com> | ||
// | ||
// 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 corefunc | ||
|
||
import "github.com/mattn/go-runewidth" | ||
|
||
/* | ||
StrByteLength returns the number of bytes/runes in a string. This is different | ||
from the number of characters, which is what Terraform and OpenTofu's `length()` | ||
function returns for strings. | ||
---- | ||
- str (string): The string with which to count bytes/runes. | ||
*/ | ||
func StrByteLength(str string) int { | ||
return runewidth.StringWidth(str) | ||
} |
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,55 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <rparman@northwood-labs.com> | ||
// | ||
// 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 corefunc | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/northwood-labs/terraform-provider-corefunc/testfixtures" | ||
) | ||
|
||
func ExampleStrByteLength() { | ||
output := StrByteLength("abcde") | ||
fmt.Println(output) | ||
|
||
output = StrByteLength("\x00") | ||
fmt.Println(output) | ||
|
||
output = StrByteLength("👁") | ||
fmt.Println(output) | ||
|
||
output = StrByteLength("■㈱の世界①") | ||
fmt.Println(output) | ||
|
||
// Output: | ||
// 5 | ||
// 0 | ||
// 1 | ||
// 10 | ||
} | ||
|
||
func TestStrByteLength(t *testing.T) { // lint:allow_complexity | ||
for name, tc := range testfixtures.StrByteLengthTestTable { | ||
t.Run(name, func(t *testing.T) { | ||
output := StrByteLength(tc.Input) | ||
|
||
if output != tc.Expected { | ||
t.Errorf("Expected %d, got %d", tc.Expected, output) | ||
} | ||
}) | ||
} | ||
} |
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,58 @@ | ||
// Copyright 2023-2024, Northwood Labs | ||
// Copyright 2023-2024, Ryan Parman <rparman@northwood-labs.com> | ||
// | ||
// 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 testfixtures // lint:no_dupe | ||
|
||
// StrByteLengthTestTable is used by both the standard Go tests and also the | ||
// Terraform acceptance tests. | ||
// <https://github.com/golang/go/wiki/TableDrivenTests> | ||
var StrByteLengthTestTable = map[string]struct { // lint:no_dupe | ||
Input string | ||
Expected int | ||
}{ | ||
"blank": {"", 0}, | ||
"abcde": {"abcde", 5}, | ||
"世": {"世", 2}, | ||
"界": {"界", 2}, | ||
"セ": {"セ", 1}, | ||
"カ": {"カ", 1}, | ||
"イ": {"イ", 1}, | ||
"☆": {"☆", 1}, | ||
"☺": {"☺", 1}, | ||
"☻": {"☻", 1}, | ||
"♥": {"♥", 1}, | ||
"♦": {"♦", 1}, | ||
"♣": {"♣", 1}, | ||
"♠": {"♠", 1}, | ||
"♂": {"♂", 1}, | ||
"♀": {"♀", 1}, | ||
"♪": {"♪", 1}, | ||
"♫": {"♫", 1}, | ||
"☼": {"☼", 1}, | ||
"↕": {"↕", 1}, | ||
"‼": {"‼", 1}, | ||
"↔": {"↔", 1}, | ||
"\x00": {"\x00", 0}, | ||
"\x01": {"\x01", 0}, | ||
"\u0300": {"\u0300", 0}, | ||
"\u2028": {"\u2028", 0}, | ||
"\u2029": {"\u2029", 0}, | ||
"a": {"a", 1}, | ||
"⟦": {"⟦", 1}, | ||
"👁": {"👁", 1}, | ||
"■㈱の世界①": {"■㈱の世界①", 10}, | ||
"スター☆": {"スター☆", 7}, | ||
"つのだ☆HIRO": {"つのだ☆HIRO", 11}, | ||
} |