pax_global_header00006660000000000000000000000064124515234320014513gustar00rootroot0000000000000052 comment=360db0db4b01d34e12a2ec042c09e7d37fece761 golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/000077500000000000000000000000001245152343200227275ustar00rootroot00000000000000golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/.travis.yml000066400000000000000000000002201245152343200250320ustar00rootroot00000000000000language: go go: - 1.3 - 1.4 - tip install: - go get -v github.com/naoina/go-stringutil script: - go test -v ./... -bench . -benchmem golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/LICENSE000066400000000000000000000020621245152343200237340ustar00rootroot00000000000000Copyright (c) 2015 Naoya Inada Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/README.md000066400000000000000000000004431245152343200242070ustar00rootroot00000000000000# stringutil [![Build Status](https://travis-ci.org/naoina/go-stringutil.png?branch=master)](https://travis-ci.org/naoina/go-stringutil) ## Installation go get -u github.com/naoina/go-stringutil ## Documentation See https://godoc.org/github.com/naoina/go-stringutil ## License MIT golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/strings.go000066400000000000000000000047201245152343200247520ustar00rootroot00000000000000package stringutil import ( "bytes" "unicode" ) // ToUpperCamelCase returns a copy of the string s with all Unicode letters mapped to their camel case. // It will convert to upper case previous letter of '_' and first letter, and remove letter of '_'. func ToUpperCamelCase(s string) string { if s == "" { return "" } upper := true var result bytes.Buffer for _, c := range s { if c == '_' { upper = true continue } if upper { result.WriteRune(unicode.ToUpper(c)) upper = false continue } result.WriteRune(c) } return result.String() } // ToUpperCamelCaseASCII is similar to ToUpperCamelCase, but optimized for // only the ASCII characters. // ToUpperCamelCaseASCII is faster than ToUpperCamelCase, but doesn't work if // contains non-ASCII characters. func ToUpperCamelCaseASCII(s string) string { if s == "" { return "" } upper := true result := make([]byte, 0, len(s)) for i := 0; i < len(s); i++ { c := s[i] if c == '_' { upper = true continue } if upper { result = append(result, toUpperASCII(c)) upper = false continue } result = append(result, c) } return string(result) } // ToSnakeCase returns a copy of the string s with all Unicode letters mapped to their snake case. // It will insert letter of '_' at position of previous letter of uppercase and all // letters convert to lower case. func ToSnakeCase(s string) string { if s == "" { return "" } var result bytes.Buffer for _, c := range s { if unicode.IsUpper(c) { result.WriteByte('_') } result.WriteRune(unicode.ToLower(c)) } s = result.String() if s[0] == '_' { return s[1:] } return s } // ToSnakeCaseASCII is similar to ToSnakeCase, but optimized for only the ASCII // characters. // ToSnakeCaseASCII is faster than ToSnakeCase, but doesn't work correctly if // contains non-ASCII characters. func ToSnakeCaseASCII(s string) string { if s == "" { return "" } result := make([]byte, 0, len(s)) for i := 0; i < len(s); i++ { c := s[i] if isUpperASCII(c) { result = append(result, '_') } result = append(result, toLowerASCII(c)) } if result[0] == '_' { return string(result[1:]) } return string(result) } func isUpperASCII(c byte) bool { return 'A' <= c && c <= 'Z' } func isLowerASCII(c byte) bool { return 'a' <= c && c <= 'z' } func toUpperASCII(c byte) byte { if isLowerASCII(c) { return c - ('a' - 'A') } return c } func toLowerASCII(c byte) byte { if isUpperASCII(c) { return c + 'a' - 'A' } return c } golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/strings_bench_test.go000066400000000000000000000013711245152343200271470ustar00rootroot00000000000000package stringutil_test import ( "testing" "github.com/naoina/go-stringutil" ) var benchcaseForCamelCase = "the_quick_brown_fox_jumps_over_the_lazy_dog" func BenchmarkToUpperCamelCase(b *testing.B) { for i := 0; i < b.N; i++ { stringutil.ToUpperCamelCase(benchcaseForCamelCase) } } func BenchmarkToUpperCamelCaseASCII(b *testing.B) { for i := 0; i < b.N; i++ { stringutil.ToUpperCamelCaseASCII(benchcaseForCamelCase) } } var benchcaseForSnakeCase = "TheQuickBrownFoxJumpsOverTheLazyDog" func BenchmarkToSnakeCase(b *testing.B) { for i := 0; i < b.N; i++ { stringutil.ToSnakeCase(benchcaseForSnakeCase) } } func BenchmarkToSnakeCaseASCII(b *testing.B) { for i := 0; i < b.N; i++ { stringutil.ToSnakeCaseASCII(benchcaseForSnakeCase) } } golang-github-naoina-go-stringutil-0.0~git20150102.360db0d/strings_test.go000066400000000000000000000070601245152343200260110ustar00rootroot00000000000000package stringutil_test import ( "reflect" "testing" "github.com/naoina/go-stringutil" ) func TestToUpperCamelCase(t *testing.T) { for _, v := range []struct { input, expect string }{ {"", ""}, {"thequickbrownfoxoverthelazydog", "Thequickbrownfoxoverthelazydog"}, {"thequickbrownfoxoverthelazydoG", "ThequickbrownfoxoverthelazydoG"}, {"thequickbrownfoxoverthelazydo_g", "ThequickbrownfoxoverthelazydoG"}, {"TheQuickBrownFoxJumpsOverTheLazyDog", "TheQuickBrownFoxJumpsOverTheLazyDog"}, {"the_quick_brown_fox_jumps_over_the_lazy_dog", "TheQuickBrownFoxJumpsOverTheLazyDog"}, {"the_Quick_Brown_Fox_Jumps_Over_The_Lazy_Dog", "TheQuickBrownFoxJumpsOverTheLazyDog"}, {"the_quick_brown_fox_over_the_lazy_dog", "TheQuickBrownFoxOverTheLazyDog"}, } { actual := stringutil.ToUpperCamelCase(v.input) expect := v.expect if !reflect.DeepEqual(actual, expect) { t.Errorf(`stringutil.ToUpperCamelCase(%#v) => %#v; want %#v`, v.input, actual, expect) } } } func TestToUpperCamelCaseASCII(t *testing.T) { for _, v := range []struct { input, expect string }{ {"", ""}, {"thequickbrownfoxoverthelazydog", "Thequickbrownfoxoverthelazydog"}, {"thequickbrownfoxoverthelazydoG", "ThequickbrownfoxoverthelazydoG"}, {"thequickbrownfoxoverthelazydo_g", "ThequickbrownfoxoverthelazydoG"}, {"TheQuickBrownFoxJumpsOverTheLazyDog", "TheQuickBrownFoxJumpsOverTheLazyDog"}, {"the_quick_brown_fox_jumps_over_the_lazy_dog", "TheQuickBrownFoxJumpsOverTheLazyDog"}, {"the_Quick_Brown_Fox_Jumps_Over_The_Lazy_Dog", "TheQuickBrownFoxJumpsOverTheLazyDog"}, } { actual := stringutil.ToUpperCamelCaseASCII(v.input) expect := v.expect if !reflect.DeepEqual(actual, expect) { t.Errorf(`stringutil.ToUpperCamelCaseASCII(%#v) => %#v; want %#v`, v.input, actual, expect) } } } func TestToSnakeCase(t *testing.T) { for _, v := range []struct { input, expect string }{ {"", ""}, {"thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"}, {"Thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"}, {"ThequickbrownfoxjumpsoverthelazydoG", "thequickbrownfoxjumpsoverthelazydo_g"}, {"TheQuickBrownFoxJumpsOverTheLazyDog", "the_quick_brown_fox_jumps_over_the_lazy_dog"}, {"the_quick_brown_fox_jumps_over_the_lazy_dog", "the_quick_brown_fox_jumps_over_the_lazy_dog"}, {"TheQuickBrownFoxOverTheLazyDog", "the_quick_brown_fox_over_the_lazy_dog"}, } { actual := stringutil.ToSnakeCase(v.input) expect := v.expect if !reflect.DeepEqual(actual, expect) { t.Errorf(`stringutil.ToSnakeCase(%#v) => %#v; want %#v`, v.input, actual, expect) } } } func TestToSnakeCaseASCII(t *testing.T) { for _, v := range []struct { input, expect string }{ {"", ""}, {"thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"}, {"Thequickbrownfoxjumpsoverthelazydog", "thequickbrownfoxjumpsoverthelazydog"}, {"ThequickbrownfoxjumpsoverthelazydoG", "thequickbrownfoxjumpsoverthelazydo_g"}, {"TheQuickBrownFoxJumpsOverTheLazyDog", "the_quick_brown_fox_jumps_over_the_lazy_dog"}, {"the_quick_brown_fox_jumps_over_the_lazy_dog", "the_quick_brown_fox_jumps_over_the_lazy_dog"}, } { actual := stringutil.ToSnakeCaseASCII(v.input) expect := v.expect if !reflect.DeepEqual(actual, expect) { t.Errorf(`stringutil.ToSnakeCaseASCII(%#v) => %#v; want %#v`, v.input, actual, expect) } } }