Browse Source

Able to encode text

Piotr Czajkowski 3 years ago
parent
commit
5a6ba6b474
1 changed files with 56 additions and 10 deletions
  1. 56 10
      app.go

+ 56 - 10
app.go

@@ -3,18 +3,64 @@ package main
 import (
 	"fmt"
 	"math/rand"
+	"unicode"
 )
 
-func main() {
-	test := "Poświadczam"
-	runes := []rune(test)
-	part := runes[1 : len(runes)-1]
-	partLength := len(part)
-	fmt.Println(string(part))
-
-	rand.Shuffle(partLength, func(i, j int) {
-		part[i], part[j] = part[j], part[i]
+func encodeWord(word []rune) []rune {
+	wordLength := len(word)
+	if wordLength < 4 {
+		return word
+	}
+
+	if wordLength == 4 {
+		word[2], word[3] = word[3], word[2]
+		return word
+	}
+
+	toShuffle := word[1 : wordLength-1]
+	toShuffleLength := wordLength - 2
+	rand.Shuffle(toShuffleLength, func(i, j int) {
+		toShuffle[i], toShuffle[j] = toShuffle[j], toShuffle[i]
 	})
 
-	fmt.Println(string(part))
+	return word
+}
+
+func processText(text []rune) string {
+	var currentWord []rune
+	var newString []rune
+
+	for _, item := range text {
+		if unicode.IsPunct(item) {
+			currentWord = encodeWord(currentWord)
+			for _, letter := range currentWord {
+				newString = append(newString, letter)
+			}
+			currentWord = []rune{}
+
+			newString = append(newString, item)
+			continue
+		}
+
+		if unicode.IsSpace(item) {
+			currentWord = encodeWord(currentWord)
+			for _, letter := range currentWord {
+				newString = append(newString, letter)
+			}
+			currentWord = []rune{}
+
+			newString = append(newString, item)
+			continue
+		}
+
+		currentWord = append(currentWord, item)
+	}
+
+	return string(newString)
+}
+
+func main() {
+	test := `This is a long looong test sentence,
+with some big (biiiiig) words!`
+	fmt.Println(processText([]rune(test)))
 }