Browse Source

Initial commit

Piotr Czajkowski 1 year ago
commit
4f341081b9
8 changed files with 140 additions and 0 deletions
  1. 17 0
      .replit
  2. 9 0
      Makefile
  3. 66 0
      chunkOfSize.go
  4. 36 0
      chunkOfSize_test.go
  5. 3 0
      go.mod
  6. BIN
      main
  7. 6 0
      replit.nix
  8. 3 0
      testText.go

+ 17 - 0
.replit

@@ -0,0 +1,17 @@
+run = ["make", "run"]
+
+# If the main file is changed, also change it in Makefile.
+entrypoint = "main.go"
+hidden = ["Makefile", "main"]
+
+[languages.go]
+pattern = "**/*.go"
+
+[languages.go.languageServer]
+start = "gopls"
+
+[nix]
+channel = "stable-21_11"
+
+[gitHubImport]
+requiredFiles = [".replit", "replit.nix"]

+ 9 - 0
Makefile

@@ -0,0 +1,9 @@
+.PHONY: run
+run: main
+	./$<
+
+main: *.go go.mod
+	go build -o $@ .
+
+.PHONY: all
+all: main

+ 66 - 0
chunkOfSize.go

@@ -0,0 +1,66 @@
+package cos
+
+import (
+  "strings"
+  "unicode/utf8"
+  "fmt"
+)
+
+// ChunkOfSize
+type ChunkOfSize struct {
+	current int
+	limit int
+	chunks []string
+	errors []string
+}
+
+// NewChunkOfSize returns new instance of ChunkOfSize initialized with given text and size
+func NewChunkOfSize(text string, size int) *ChunkOfSize {
+	return &ChunkOfSize{
+		current: 0,
+		limit: size,
+		chunks: strings.Split(text, " "),
+	}
+}
+
+// Next returns next chunk of text or empty string if nothing left to process
+func (c *ChunkOfSize) Next() string {
+	var b strings.Builder
+	for i := range c.chunks {
+		l := utf8.RuneCountInString(c.chunks[i])
+		if l + c.current >= c.limit {
+			c.current = 0
+			c.chunks = c.chunks[i:]
+			return b.String()
+		}
+
+		var toWrite string
+		if c.current == 0 || c.chunks[i] == "\n" {
+			toWrite = c.chunks[i]
+		} else {
+			toWrite = fmt.Sprintf(" %s", c.chunks[i])
+			l++
+		}
+
+		w, err := b.WriteString(toWrite)
+		if w == 0 || err != nil {
+			c.errors = append(c.errors, fmt.Sprintf("Error writing: %s; %s", toWrite, err))
+			continue
+		}
+
+		c.current += l
+	}
+
+	c.chunks = []string{}
+	return b.String()
+}
+
+// Success returns true if there are no errors
+func (c *ChunkOfSize) Success() bool {
+	return len(c.errors) == 0
+}
+
+// GetErrors returns erorrs
+func (c *ChunkOfSize) GetErrors() []string {
+  return c.errors
+}

+ 36 - 0
chunkOfSize_test.go

@@ -0,0 +1,36 @@
+package cos
+
+import (
+	"strings"
+	"testing"
+	"unicode/utf8"
+)
+
+func Test4ChunksOf100(t *testing.T) {
+	size := 100
+  expectedChunks := 4
+  
+	chunk := NewChunkOfSize(testText, size)
+	count := 0
+
+	for {
+		text := chunk.Next()
+		if text == "" {
+			break
+		}
+
+		if utf8.RuneCountInString(text) > size {
+			t.Fatal(text, "\nis longer than", size)
+		}
+
+		count++
+	}
+  
+	if count != expectedChunks {
+		t.Fatal("There should be", expectedChunks, "chunks, but have", count)
+	}
+
+	if !chunk.Success() {
+		t.Fatal("There were errors:\n", strings.Join(chunk.GetErrors(), "\n"))
+	}
+}

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module cos
+
+go 1.17

BIN
main


+ 6 - 0
replit.nix

@@ -0,0 +1,6 @@
+{ pkgs }: {
+    deps = [
+        pkgs.go_1_17
+        pkgs.gopls
+    ];
+}

+ 3 - 0
testText.go

@@ -0,0 +1,3 @@
+package cos
+
+var testText string = "Fuga ullam porro iusto est quaerat accusantium laboriosam consequuntur, a optio repellendus error! corrupti neque, quasi suscipit aliquam architecto ratione delectus. placeat dicta saepe cumque. Ipsam officiis asperiores rerum vel nam beatae ipsa tempora! eius! officia, deserunt, vero voluptatum ad"