Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Go):add slashes #51

Merged
merged 8 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions go/add_slashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pehape

// AddSlashes returns a string with backslashes added before characters that need to be escaped. These characters are: \\ " and \'
func AddSlashes(str string) string {
var tmpRune []rune
strRune := []rune(str)
for _, ch := range strRune {
switch ch {
case []rune{'\\'}[0], []rune{'"'}[0], []rune{'\''}[0]:
tmpRune = append(tmpRune, []rune{'\\'}[0])
tmpRune = append(tmpRune, ch)
default:
tmpRune = append(tmpRune, ch)
}
}
return string(tmpRune)
}
20 changes: 20 additions & 0 deletions go/add_slashes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pehape_test

import (
"testing"

PHP "github.com/teknologi-umum/pehape/go"
)

func TestAddSlashes(t *testing.T) {
t.Run("double quote", func(t *testing.T) {
if res := PHP.AddSlashes(`nano"nan"ya`); res != `nano\"nan\"ya` {
t.Errorf(`expected nano\"nan\"ya, got %s`, res)
}
})
t.Run("slash", func(t *testing.T) {
if res := PHP.AddSlashes(`nano\nan\ya`); res != `nano\\nan\\ya` {
t.Errorf(`expected nano\\nan\\ya, got %s`, res)
}
})
}
8 changes: 7 additions & 1 deletion go/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
fmt.Println(pehape.Levenshtein(str1, str2, insertionCost, replacementCost, deletionCost))
//result : 5
```
* `Add Slashes`
- sample string
```go
pehape.Implode(`What does "yolo" mean?`)
fadilaaaa marked this conversation as resolved.
Show resolved Hide resolved
//result : What does \"yolo\" mean?
```

* `Implode`
- sample string
Expand Down Expand Up @@ -69,4 +75,4 @@ fmt.Println(pehape.Ucwords(bar))
fmt.Println(pehape.Strrev(bar))
//result : "kasur rusak"
```