Skip to content

Commit

Permalink
Adds helper functions for strings and hashes. Closes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
binaek authored Nov 22, 2022
1 parent 5d0c50f commit c0d7dea
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
54 changes: 54 additions & 0 deletions helpers/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package helpers

import (
"bytes"
"crypto/md5"
"encoding/hex"
"hash/fnv"
"io"
"os"
)

// FileMD5Hash streams a file into a MD5 hasher and returns the resultant MD5 hash bytes
// This DOES NOT read the whole file into memory
func FileMD5Hash(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

hasher := md5.New()
if _, err := io.Copy(hasher, file); err != nil {
return nil, err
}
return hasher.Sum(nil), nil
}

// FileHash streams a file into a MD5 hasher and returns the resultant MD5 hash
// as a HEX encoded string. Uses FileMD5Hash under-the-hood
func FileHash(filePath string) (string, error) {
hash, err := FileMD5Hash(filePath)
if err != nil {
return "", err
}
return hex.EncodeToString(hash), nil
}

// StringFnvHash returns a FNV1 hash of the given string.
// returns 0 if there's an error
func StringFnvHash(s string) uint32 {
b := bytes.NewBufferString(s)
h := fnv.New32a()
if _, err := io.Copy(h, b); err != nil {
return 0
}
return h.Sum32()
}

// GetMD5Hash returns the MD5 hash of the given string
func GetMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
h := hex.EncodeToString(hash[:])
return h
}
50 changes: 50 additions & 0 deletions helpers/string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package helpers

import (
"bytes"
"encoding/csv"
"fmt"
"strings"
"unicode"
Expand Down Expand Up @@ -83,3 +85,51 @@ func Clean(str string) string {
return -1
}, str)
}

// SplitByRune uses the CSV decoder to parse out the tokens - even if they are quoted and/or escaped
func SplitByRune(str string, r rune) []string {
csvDecoder := csv.NewReader(strings.NewReader(str))
csvDecoder.Comma = r
csvDecoder.LazyQuotes = true
csvDecoder.TrimLeadingSpace = true
split, _ := csvDecoder.Read()
return split
}

// SplitByWhitespace splits by the ' ' rune
func SplitByWhitespace(str string) []string {
return SplitByRune(str, ' ')
}

// Resize resizes the string with the given length. It ellipses with '…' when the string's length exceeds
// the desired length or pads spaces to the right of the string when length is smaller than desired
func Resize(s string, length uint) string {
n := int(length)
if len(s) == n {
return s
}
// Pads only when length of the string smaller than len needed
s = PadRight(s, n, ' ')
if len(s) > n {
b := []byte(s)
var buf bytes.Buffer
for i := 0; i < n-1; i++ {
buf.WriteByte(b[i])
}
buf.WriteString("…")
s = buf.String()
}
return s
}

// PadRight returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.
func PadRight(str string, length int, pad byte) string {
if len(str) >= length {
return str
}
buf := bytes.NewBufferString(str)
for i := 0; i < length-len(str); i++ {
buf.WriteByte(pad)
}
return buf.String()
}

0 comments on commit c0d7dea

Please sign in to comment.