Skip to content

Commit

Permalink
feat: Add support for counting bytes in strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
skyzyx committed Nov 12, 2024
1 parent 79d1647 commit 3ddd389
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
31 changes: 31 additions & 0 deletions corefunc/str_byte_length.go
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)
}
55 changes: 55 additions & 0 deletions corefunc/str_byte_length_test.go
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)
}
})
}
}
58 changes: 58 additions & 0 deletions testfixtures/str_byte_length.go
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},
}

0 comments on commit 3ddd389

Please sign in to comment.