Skip to content

Commit

Permalink
Merge pull request #39 from kumparan/feature/escape-quote
Browse files Browse the repository at this point in the history
feature: escape quote
  • Loading branch information
zipzap11 authored Aug 11, 2022
2 parents f6e7166 + 2de0da7 commit 45a0120
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
25 changes: 16 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# go-utils

<a name="v1.25.0"></a>
## [v1.25.0] - 2022-08-10
### New Features
- escape quote


<a name="v1.24.0"></a>
## [v1.24.0] - 2022-07-15
### New Features
- nanoseconds check on rpc3339nano time formatting
- nanoseconds check on rfc3339nano time formatting ([#38](https://github.com/kumparan/kumnats/issues/38))


<a name="v1.23.0"></a>
Expand All @@ -13,7 +19,7 @@
- add int64 pointer to int64 conversion ([#33](https://github.com/kumparan/kumnats/issues/33))

### Other Improvements
- fix dependabot alert
- fix dependabot alert ([#37](https://github.com/kumparan/kumnats/issues/37))


<a name="v1.22.0"></a>
Expand All @@ -34,11 +40,11 @@
- fix marshal issue on gorm.DeletedAt empty value ([#32](https://github.com/kumparan/kumnats/issues/32))


<a name="v1.20.0"></a>
## [v1.20.0] - 2022-03-11

<a name="v.1.20.0"></a>
## [v.1.20.0] - 2022-03-11

<a name="v1.20.0"></a>
## [v1.20.0] - 2022-03-11
### New Features
- add constraint size gql directive ([#30](https://github.com/kumparan/kumnats/issues/30))

Expand Down Expand Up @@ -205,14 +211,15 @@
- init go-utils


[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.24.0...HEAD
[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.25.0...HEAD
[v1.25.0]: https://github.com/kumparan/kumnats/compare/v1.24.0...v1.25.0
[v1.24.0]: https://github.com/kumparan/kumnats/compare/v1.23.0...v1.24.0
[v1.23.0]: https://github.com/kumparan/kumnats/compare/v1.22.0...v1.23.0
[v1.22.0]: https://github.com/kumparan/kumnats/compare/v1.21.0...v1.22.0
[v1.21.0]: https://github.com/kumparan/kumnats/compare/v1.20.1...v1.21.0
[v1.20.1]: https://github.com/kumparan/kumnats/compare/v1.20.0...v1.20.1
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v.1.20.0...v1.20.0
[v.1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v.1.20.0
[v1.20.1]: https://github.com/kumparan/kumnats/compare/v.1.20.0...v1.20.1
[v.1.20.0]: https://github.com/kumparan/kumnats/compare/v1.20.0...v.1.20.0
[v1.20.0]: https://github.com/kumparan/kumnats/compare/v1.19.3...v1.20.0
[v1.19.3]: https://github.com/kumparan/kumnats/compare/v1.19.2...v1.19.3
[v1.19.2]: https://github.com/kumparan/kumnats/compare/v1.19.1...v1.19.2
[v1.19.1]: https://github.com/kumparan/kumnats/compare/v1.19.0...v1.19.1
Expand Down
15 changes: 15 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ func TruncateString(str string, num int) string {
}
return bnoden
}

func EscapeQuote(in string) string {
res := []byte{}
const (
doubleQuoteASCII = 34
backSlashASCII = 92
)
for i := 0; i < len(in); i++ {
if in[i] == doubleQuoteASCII && ((i > 0 && in[i-1] != backSlashASCII) || i == 0) {
res = append(res, backSlashASCII)
}
res = append(res, in[i])
}
return string(res)
}
16 changes: 16 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,19 @@ func Test_TruncateString(t *testing.T) {
assert.Equal(t, "Ab", TruncateString("Absolutely Longer", 2))
assert.Equal(t, "A...", TruncateString("Absolutely Longer", 4))
}

func Test_EscapeQuote(t *testing.T) {
strWithoutQuote := "this is brazil"
strWithQuote := "\"bek bek bek\""
strWithEscapedQuote := "\\\"bek\\\" bek\\\" bek\\\""

testCases := map[string]string{
strWithoutQuote: "this is brazil",
strWithQuote: "\\\"bek bek bek\\\"",
strWithEscapedQuote: "\\\"bek\\\" bek\\\" bek\\\"",
}

for in, out := range testCases {
assert.Equal(t, out, EscapeQuote(in))
}
}

0 comments on commit 45a0120

Please sign in to comment.