-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hmsayem <[email protected]>
- Loading branch information
Showing
6 changed files
with
218 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package strings | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func Fmt(s string) string { | ||
stripper := &Stripe{ | ||
Result: "", | ||
} | ||
stripper.Write(s) | ||
return stripper.Result | ||
} | ||
|
||
// Striplines wraps an output stream, stripping runs of consecutive empty lines. | ||
// You must call Flush before the output stream will be complete. | ||
// Implements io.WriteCloser, Writer, Closer. | ||
type Stripe struct { | ||
Result string | ||
lastLine []byte | ||
currentLine []byte | ||
} | ||
|
||
func (w *Stripe) Write(p string) (int, error) { | ||
totalN := 0 | ||
s := string(p) | ||
if !strings.Contains(s, "\n") { | ||
w.currentLine = append(w.currentLine, p...) | ||
return 0, nil | ||
} | ||
cur := string(append(w.currentLine, p...)) | ||
lastN := strings.LastIndex(cur, "\n") | ||
s = cur[:lastN] | ||
for _, line := range strings.Split(s, "\n") { | ||
n, err := w.writeLn(line + "\n") | ||
w.lastLine = []byte(line) | ||
if err != nil { | ||
return totalN, err | ||
} | ||
totalN += n | ||
} | ||
rem := cur[(lastN + 1):] | ||
w.currentLine = []byte(rem) | ||
return totalN, nil | ||
} | ||
|
||
// Close flushes the last of the output into the underlying writer. | ||
func (w *Stripe) Close() error { | ||
_, err := w.writeLn(string(w.currentLine)) | ||
return err | ||
} | ||
|
||
func (w *Stripe) writeLn(line string) (n int, err error) { | ||
if strings.TrimSpace(string(w.lastLine)) == "" && strings.TrimSpace(line) == "" { | ||
return 0, nil | ||
} else { | ||
w.Result = w.Result + line | ||
return len(line), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package strings | ||
|
||
import "log" | ||
|
||
func VString(def string, args ...string) string { | ||
v := def | ||
if len(args) == 1 { | ||
v = args[0] | ||
} else if len(args) > 1 { | ||
v = args[0] | ||
log.Printf("Found more than 1 argument when expected 1 %v", args) | ||
} | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package strings | ||
|
||
import "strings" | ||
|
||
func Val(v string, def string) string { | ||
trimmed := strings.TrimSpace(v) | ||
if trimmed == "" { | ||
return def | ||
} | ||
return trimmed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package strings | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
// Benchmark 19246 ns/op. | ||
func Reverse(s string) string { | ||
size := len(s) | ||
buf := make([]byte, size) | ||
for start := 0; start < size; { | ||
r, n := utf8.DecodeRuneInString(s[start:]) | ||
start += n | ||
utf8.EncodeRune(buf[size-start:], r) | ||
} | ||
return string(buf) | ||
} | ||
|
||
func PrefixFold(s, prefix string) bool { | ||
return len(s) >= len(prefix) && strings.EqualFold(prefix, s[:len(prefix)]) | ||
} | ||
|
||
func IsEmpty(s *string) bool { | ||
return s == nil || *s == "" | ||
} | ||
|
||
func IsBothAlphaNum(a string) bool { | ||
alpha := false | ||
num := false | ||
for _, c := range a { | ||
if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') { | ||
alpha = true | ||
} else if c >= '0' && c <= '9' { | ||
num = true | ||
} | ||
if alpha && num { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func Contains(a []string, e string) bool { | ||
for _, s := range a { | ||
if s == e { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Allowed char: [a-z0-9]([a-z0-9-]*[a-z0-9])? | ||
// Makes it safe as a subdomain | ||
func DomainForm(s string) string { | ||
runes := make([]rune, len(s)) | ||
for i, r := range strings.ToLower(s) { | ||
if (r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || (r == '-') || (r == '.') { | ||
runes[i] = r | ||
} else if r == '_' { | ||
runes[i] = '-' // _ --> - | ||
} | ||
} | ||
return strings.Trim(string(runes), "-") | ||
} | ||
|
||
func Filter(s []string, f func(string) bool) []string { | ||
ret := make([]string, 0) | ||
for _, elm := range s { | ||
if !f(elm) { | ||
ret = append(ret, elm) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
func Join(a []*string, sep string) string { | ||
b := make([]string, len(a)) | ||
for i, s := range a { | ||
b[i] = *s | ||
} | ||
return strings.Join(b, sep) | ||
} | ||
|
||
func EqualSlice(a, b []string) bool { | ||
if a == nil && b == nil { | ||
return true | ||
} | ||
|
||
if a == nil || b == nil { | ||
return false | ||
} | ||
|
||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
// Copy slices | ||
aCopy := append([]string(nil), a...) | ||
bCopy := append([]string(nil), b...) | ||
|
||
sort.Strings(aCopy) | ||
sort.Strings(bCopy) | ||
|
||
for i := range aCopy { | ||
if aCopy[i] != bCopy[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters