Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 07ea97219d
Fetching contributors…

Cannot retrieve contributors at this time

file 157 lines (139 sloc) 3.045 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
package utils

import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"path"
"strings"
"unicode"
)

//func FormatName(name string) string {
// result := make([]byte, len(name))
// first := true
// for _, c := range name {
// if first {
// c
// } else {
// }
// }
// return string(result)
//}

func TextRowsAndCols(text string) (rows, cols int) {
var x int
for _, c := range text {
if unicode.IsPrint(c) {
switch c {
case '\n':
rows++
x = 0
case '\r':
// do nothing
default:
x++
if x > cols {
cols = x
}
}
}
}
return rows, cols
}

func StringIn(needle string, heystack []string) bool {
for _, s := range heystack {
if s == needle {
return true
}
}
return false
}

func ReverseStringSlice(slice []string) {
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
slice[i], slice[j] = slice[j], slice[i]
}
}

func PrettifyJSON(compactJSON []byte) string {
var buf bytes.Buffer
err := json.Indent(&buf, compactJSON, "", "\t")
if err != nil {
return err.Error()
}
return buf.String()
}

func MD5(data string) string {
hash := md5.New()
hash.Write([]byte(data))
return fmt.Sprintf("%x", hash.Sum(nil))
}

// Todo something more generic
func StringForStruct(typeName string, attributes ...string) string {
var builder StringBuilder

builder.Write(typeName, "{")
for i, attr := range attributes {
if i > 0 {
if i&1 == 0 {
builder.Write(", ")
} else {
builder.Write(": ")
}
}
builder.Write(attr)
}
builder.Byte('}')

return builder.String()
}

func HasImageFileExt(filename string) bool {
ext := path.Ext(strings.ToLower(filename))
return ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif"
}

func IsImageURL(url string) bool {
return HasImageFileExt(url) || strings.HasPrefix(url, "data:image/")
}

func JoinNonEmptyStrings(sep string, strings ...string) string {
var buf bytes.Buffer
first := true
for _, s := range strings {
if s != "" {
if first {
first = false
} else {
buf.WriteString(sep)
}
buf.WriteString(s)
}
}
return buf.String()
}

func CompareStringsCaseInsensitive(a, b string) bool {
return bytes.Compare(bytes.ToLower([]byte(a)), bytes.ToLower([]byte(b))) < 0
}

func EscapeJSON(jsonString string) string {
jsonString = strings.Replace(jsonString, `\`, `\\`, -1)
jsonString = strings.Replace(jsonString, `"`, `\"`, -1)
return jsonString
}

func NewLineToHTML(text string) (html string) {
return strings.Replace(text, "\n", "<br/>", -1)
}

func StripHTMLTags(text string) (plainText string) {
chars := []byte(text)
tagStart := -1
for i := 0; i < len(chars); i++ {
if chars[i] == '<' {
tagStart = i
} else if chars[i] == '>' && tagStart != -1 {
chars = append(chars[:tagStart], chars[i+1:]...)
i, tagStart = tagStart-1, -1
}
}
return string(chars)
}

func AddUrlParam(url, name, value string) string {
var separator string
if strings.Index(url, "?") == -1 {
separator = "?"
} else {
separator = "&"
}
return url + separator + name + "=" + value
}
Something went wrong with that request. Please try again.