podman
240 строк · 6.3 Кб
1/*
2Copyright 2014 Alexander Okoli
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package goutils18
19import (20"bytes"21"fmt"22"strings"23"unicode"24)
25
26// Typically returned by functions where a searched item cannot be found
27const INDEX_NOT_FOUND = -128
29/*
30Abbreviate abbreviates a string using ellipses. This will turn the string "Now is the time for all good men" into "Now is the time for..."
31
32Specifically, the algorithm is as follows:
33
34- If str is less than maxWidth characters long, return it.
35- Else abbreviate it to (str[0:maxWidth - 3] + "...").
36- If maxWidth is less than 4, return an illegal argument error.
37- In no case will it return a string of length greater than maxWidth.
38
39Parameters:
40str - the string to check
41maxWidth - maximum length of result string, must be at least 4
42
43Returns:
44string - abbreviated string
45error - if the width is too small
46*/
47func Abbreviate(str string, maxWidth int) (string, error) {48return AbbreviateFull(str, 0, maxWidth)49}
50
51/*
52AbbreviateFull abbreviates a string using ellipses. This will turn the string "Now is the time for all good men" into "...is the time for..."
53This function works like Abbreviate(string, int), but allows you to specify a "left edge" offset. Note that this left edge is not
54necessarily going to be the leftmost character in the result, or the first character following the ellipses, but it will appear
55somewhere in the result.
56In no case will it return a string of length greater than maxWidth.
57
58Parameters:
59str - the string to check
60offset - left edge of source string
61maxWidth - maximum length of result string, must be at least 4
62
63Returns:
64string - abbreviated string
65error - if the width is too small
66*/
67func AbbreviateFull(str string, offset int, maxWidth int) (string, error) {68if str == "" {69return "", nil70}71if maxWidth < 4 {72err := fmt.Errorf("stringutils illegal argument: Minimum abbreviation width is 4")73return "", err74}75if len(str) <= maxWidth {76return str, nil77}78if offset > len(str) {79offset = len(str)80}81if len(str)-offset < (maxWidth - 3) { // 15 - 5 < 10 - 3 = 10 < 782offset = len(str) - (maxWidth - 3)83}84abrevMarker := "..."85if offset <= 4 {86return str[0:maxWidth-3] + abrevMarker, nil // str.substring(0, maxWidth - 3) + abrevMarker;87}88if maxWidth < 7 {89err := fmt.Errorf("stringutils illegal argument: Minimum abbreviation width with offset is 7")90return "", err91}92if (offset + maxWidth - 3) < len(str) { // 5 + (10-3) < 15 = 12 < 1593abrevStr, _ := Abbreviate(str[offset:len(str)], (maxWidth - 3))94return abrevMarker + abrevStr, nil // abrevMarker + abbreviate(str.substring(offset), maxWidth - 3);95}96return abrevMarker + str[(len(str)-(maxWidth-3)):len(str)], nil // abrevMarker + str.substring(str.length() - (maxWidth - 3));97}
98
99/*
100DeleteWhiteSpace deletes all whitespaces from a string as defined by unicode.IsSpace(rune).
101It returns the string without whitespaces.
102
103Parameter:
104str - the string to delete whitespace from, may be nil
105
106Returns:
107the string without whitespaces
108*/
109func DeleteWhiteSpace(str string) string {110if str == "" {111return str112}113sz := len(str)114var chs bytes.Buffer115count := 0116for i := 0; i < sz; i++ {117ch := rune(str[i])118if !unicode.IsSpace(ch) {119chs.WriteRune(ch)120count++121}122}123if count == sz {124return str125}126return chs.String()127}
128
129/*
130IndexOfDifference compares two strings, and returns the index at which the strings begin to differ.
131
132Parameters:
133str1 - the first string
134str2 - the second string
135
136Returns:
137the index where str1 and str2 begin to differ; -1 if they are equal
138*/
139func IndexOfDifference(str1 string, str2 string) int {140if str1 == str2 {141return INDEX_NOT_FOUND142}143if IsEmpty(str1) || IsEmpty(str2) {144return 0145}146var i int147for i = 0; i < len(str1) && i < len(str2); i++ {148if rune(str1[i]) != rune(str2[i]) {149break150}151}152if i < len(str2) || i < len(str1) {153return i154}155return INDEX_NOT_FOUND156}
157
158/*
159IsBlank checks if a string is whitespace or empty (""). Observe the following behavior:
160
161goutils.IsBlank("") = true
162goutils.IsBlank(" ") = true
163goutils.IsBlank("bob") = false
164goutils.IsBlank(" bob ") = false
165
166Parameter:
167str - the string to check
168
169Returns:
170true - if the string is whitespace or empty ("")
171*/
172func IsBlank(str string) bool {173strLen := len(str)174if str == "" || strLen == 0 {175return true176}177for i := 0; i < strLen; i++ {178if unicode.IsSpace(rune(str[i])) == false {179return false180}181}182return true183}
184
185/*
186IndexOf returns the index of the first instance of sub in str, with the search beginning from the
187index start point specified. -1 is returned if sub is not present in str.
188
189An empty string ("") will return -1 (INDEX_NOT_FOUND). A negative start position is treated as zero.
190A start position greater than the string length returns -1.
191
192Parameters:
193str - the string to check
194sub - the substring to find
195start - the start position; negative treated as zero
196
197Returns:
198the first index where the sub string was found (always >= start)
199*/
200func IndexOf(str string, sub string, start int) int {201
202if start < 0 {203start = 0204}205
206if len(str) < start {207return INDEX_NOT_FOUND208}209
210if IsEmpty(str) || IsEmpty(sub) {211return INDEX_NOT_FOUND212}213
214partialIndex := strings.Index(str[start:len(str)], sub)215if partialIndex == -1 {216return INDEX_NOT_FOUND217}218return partialIndex + start219}
220
221// IsEmpty checks if a string is empty (""). Returns true if empty, and false otherwise.
222func IsEmpty(str string) bool {223return len(str) == 0224}
225
226// Returns either the passed in string, or if the string is empty, the value of defaultStr.
227func DefaultString(str string, defaultStr string) string {228if IsEmpty(str) {229return defaultStr230}231return str232}
233
234// Returns either the passed in string, or if the string is whitespace, empty (""), the value of defaultStr.
235func DefaultIfBlank(str string, defaultStr string) string {236if IsBlank(str) {237return defaultStr238}239return str240}
241