From 3be68d33335942ffa9d0dd01da48411ec750bd92 Mon Sep 17 00:00:00 2001 From: dila Date: Sun, 2 Oct 2022 00:22:30 +0700 Subject: [PATCH 1/7] feat:add_slashes --- go/add_slashes.go | 16 ++++++++++++++++ go/add_slashes_test.go | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 go/add_slashes.go create mode 100644 go/add_slashes_test.go diff --git a/go/add_slashes.go b/go/add_slashes.go new file mode 100644 index 0000000..54490d2 --- /dev/null +++ b/go/add_slashes.go @@ -0,0 +1,16 @@ +package pehape + +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) +} diff --git a/go/add_slashes_test.go b/go/add_slashes_test.go new file mode 100644 index 0000000..8ee233e --- /dev/null +++ b/go/add_slashes_test.go @@ -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) + } + }) +} From d42a112877be2f856f95206f50782d35d0150011 Mon Sep 17 00:00:00 2001 From: fadilaaaa <57476855+fadilaaaa@users.noreply.github.com> Date: Sun, 2 Oct 2022 18:22:16 +0700 Subject: [PATCH 2/7] feat:add doc --- go/add_slashes.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/go/add_slashes.go b/go/add_slashes.go index 54490d2..521d044 100644 --- a/go/add_slashes.go +++ b/go/add_slashes.go @@ -1,6 +1,7 @@ package pehape -func Addslashes(str string) string { +//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 { From 88ad1459e42b5473fdf9ea1eaa2706c7cd1b1c5d Mon Sep 17 00:00:00 2001 From: fadilaaaa <57476855+fadilaaaa@users.noreply.github.com> Date: Sun, 2 Oct 2022 18:22:46 +0700 Subject: [PATCH 3/7] Update add_slashes_test.go --- go/add_slashes_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/add_slashes_test.go b/go/add_slashes_test.go index 8ee233e..4bb84f5 100644 --- a/go/add_slashes_test.go +++ b/go/add_slashes_test.go @@ -8,12 +8,12 @@ import ( func TestAddSlashes(t *testing.T) { t.Run("double quote", func(t *testing.T) { - if res := PHP.Addslashes(`nano"nan"ya`); res != `nano\"nan\"ya` { + 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` { + if res := PHP.AddSlashes(`nano\nan\ya`); res != `nano\\nan\\ya` { t.Errorf(`expected nano\\nan\\ya, got %s`, res) } }) From 8682934556c454485b0f75361a9d539d7b69fa2d Mon Sep 17 00:00:00 2001 From: fadilaaaa <57476855+fadilaaaa@users.noreply.github.com> Date: Tue, 4 Oct 2022 12:07:54 +0700 Subject: [PATCH 4/7] Update go/add_slashes.go Co-authored-by: Reinaldy Rafli --- go/add_slashes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/add_slashes.go b/go/add_slashes.go index 521d044..a4e998b 100644 --- a/go/add_slashes.go +++ b/go/add_slashes.go @@ -1,6 +1,6 @@ package pehape -//Returns a string with backslashes added before characters that need to be escaped. These characters are: \\ " and \' +// 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) From 7c26b270efaeb4ad8037ac52a8f5a058ba10d0fc Mon Sep 17 00:00:00 2001 From: fadilaaaa <57476855+fadilaaaa@users.noreply.github.com> Date: Tue, 4 Oct 2022 12:08:05 +0700 Subject: [PATCH 5/7] Update go/levenshtein.go Co-authored-by: Reinaldy Rafli --- go/levenshtein.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/levenshtein.go b/go/levenshtein.go index e598f81..cfa0d21 100644 --- a/go/levenshtein.go +++ b/go/levenshtein.go @@ -1,6 +1,6 @@ package pehape -// LevenshteinDistance calculate the distance between two string +// Levenshtein calculates the distance between two strings // This algorithm allow insertions, deletions and substitutions to change one string to the second // Compatible with non-ASCII characters func Levenshtein(str1 string, str2 string, cost ...int) int { From ae09423bb32ce0e3f9e278f8c37aa5307268cdd0 Mon Sep 17 00:00:00 2001 From: fadilaaaa <57476855+fadilaaaa@users.noreply.github.com> Date: Tue, 4 Oct 2022 14:01:51 +0700 Subject: [PATCH 6/7] Update readme.md --- go/readme.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/go/readme.md b/go/readme.md index 28f8875..198a766 100644 --- a/go/readme.md +++ b/go/readme.md @@ -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?`) +//result : What does \"yolo\" mean? +``` * `Implode` - sample string @@ -69,4 +75,4 @@ fmt.Println(pehape.Ucwords(bar)) fmt.Println(pehape.Strrev(bar)) //result : "kasur rusak" ``` - \ No newline at end of file + From 363ef93e9f698795aaab325cef61871f51b8d131 Mon Sep 17 00:00:00 2001 From: fadilaaaa <57476855+fadilaaaa@users.noreply.github.com> Date: Tue, 4 Oct 2022 15:36:34 +0700 Subject: [PATCH 7/7] Update readme.md --- go/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/readme.md b/go/readme.md index 198a766..54c3a14 100644 --- a/go/readme.md +++ b/go/readme.md @@ -22,7 +22,7 @@ * `Add Slashes` - sample string ```go -pehape.Implode(`What does "yolo" mean?`) +pehape.AddSlashes(`What does "yolo" mean?`) //result : What does \"yolo\" mean? ```