wordutils.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. Copyright 2014 Alexander Okoli
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. Package goutils provides utility functions to manipulate strings in various ways.
  15. The code snippets below show examples of how to use goutils. Some functions return
  16. errors while others do not, so usage would vary as a result.
  17. Example:
  18. package main
  19. import (
  20. "fmt"
  21. "github.com/aokoli/goutils"
  22. )
  23. func main() {
  24. // EXAMPLE 1: A goutils function which returns no errors
  25. fmt.Println (goutils.Initials("John Doe Foo")) // Prints out "JDF"
  26. // EXAMPLE 2: A goutils function which returns an error
  27. rand1, err1 := goutils.Random (-1, 0, 0, true, true)
  28. if err1 != nil {
  29. fmt.Println(err1) // Prints out error message because -1 was entered as the first parameter in goutils.Random(...)
  30. } else {
  31. fmt.Println(rand1)
  32. }
  33. }
  34. */
  35. package goutils
  36. import (
  37. "bytes"
  38. "strings"
  39. "unicode"
  40. )
  41. // VERSION indicates the current version of goutils
  42. const VERSION = "1.0.0"
  43. /*
  44. Wrap wraps a single line of text, identifying words by ' '.
  45. New lines will be separated by '\n'. Very long words, such as URLs will not be wrapped.
  46. Leading spaces on a new line are stripped. Trailing spaces are not stripped.
  47. Parameters:
  48. str - the string to be word wrapped
  49. wrapLength - the column (a column can fit only one character) to wrap the words at, less than 1 is treated as 1
  50. Returns:
  51. a line with newlines inserted
  52. */
  53. func Wrap(str string, wrapLength int) string {
  54. return WrapCustom(str, wrapLength, "", false)
  55. }
  56. /*
  57. WrapCustom wraps a single line of text, identifying words by ' '.
  58. Leading spaces on a new line are stripped. Trailing spaces are not stripped.
  59. Parameters:
  60. str - the string to be word wrapped
  61. wrapLength - the column number (a column can fit only one character) to wrap the words at, less than 1 is treated as 1
  62. newLineStr - the string to insert for a new line, "" uses '\n'
  63. wrapLongWords - true if long words (such as URLs) should be wrapped
  64. Returns:
  65. a line with newlines inserted
  66. */
  67. func WrapCustom(str string, wrapLength int, newLineStr string, wrapLongWords bool) string {
  68. if str == "" {
  69. return ""
  70. }
  71. if newLineStr == "" {
  72. newLineStr = "\n" // TODO Assumes "\n" is seperator. Explore SystemUtils.LINE_SEPARATOR from Apache Commons
  73. }
  74. if wrapLength < 1 {
  75. wrapLength = 1
  76. }
  77. inputLineLength := len(str)
  78. offset := 0
  79. var wrappedLine bytes.Buffer
  80. for inputLineLength-offset > wrapLength {
  81. if rune(str[offset]) == ' ' {
  82. offset++
  83. continue
  84. }
  85. end := wrapLength + offset + 1
  86. spaceToWrapAt := strings.LastIndex(str[offset:end], " ") + offset
  87. if spaceToWrapAt >= offset {
  88. // normal word (not longer than wrapLength)
  89. wrappedLine.WriteString(str[offset:spaceToWrapAt])
  90. wrappedLine.WriteString(newLineStr)
  91. offset = spaceToWrapAt + 1
  92. } else {
  93. // long word or URL
  94. if wrapLongWords {
  95. end := wrapLength + offset
  96. // long words are wrapped one line at a time
  97. wrappedLine.WriteString(str[offset:end])
  98. wrappedLine.WriteString(newLineStr)
  99. offset += wrapLength
  100. } else {
  101. // long words aren't wrapped, just extended beyond limit
  102. end := wrapLength + offset
  103. spaceToWrapAt = strings.IndexRune(str[end:len(str)], ' ') + end
  104. if spaceToWrapAt >= 0 {
  105. wrappedLine.WriteString(str[offset:spaceToWrapAt])
  106. wrappedLine.WriteString(newLineStr)
  107. offset = spaceToWrapAt + 1
  108. } else {
  109. wrappedLine.WriteString(str[offset:len(str)])
  110. offset = inputLineLength
  111. }
  112. }
  113. }
  114. }
  115. wrappedLine.WriteString(str[offset:len(str)])
  116. return wrappedLine.String()
  117. }
  118. /*
  119. Capitalize capitalizes all the delimiter separated words in a string. Only the first letter of each word is changed.
  120. To convert the rest of each word to lowercase at the same time, use CapitalizeFully(str string, delimiters ...rune).
  121. The delimiters represent a set of characters understood to separate words. The first string character
  122. and the first non-delimiter character after a delimiter will be capitalized. A "" input string returns "".
  123. Capitalization uses the Unicode title case, normally equivalent to upper case.
  124. Parameters:
  125. str - the string to capitalize
  126. delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
  127. Returns:
  128. capitalized string
  129. */
  130. func Capitalize(str string, delimiters ...rune) string {
  131. var delimLen int
  132. if delimiters == nil {
  133. delimLen = -1
  134. } else {
  135. delimLen = len(delimiters)
  136. }
  137. if str == "" || delimLen == 0 {
  138. return str
  139. }
  140. buffer := []rune(str)
  141. capitalizeNext := true
  142. for i := 0; i < len(buffer); i++ {
  143. ch := buffer[i]
  144. if isDelimiter(ch, delimiters...) {
  145. capitalizeNext = true
  146. } else if capitalizeNext {
  147. buffer[i] = unicode.ToTitle(ch)
  148. capitalizeNext = false
  149. }
  150. }
  151. return string(buffer)
  152. }
  153. /*
  154. CapitalizeFully converts all the delimiter separated words in a string into capitalized words, that is each word is made up of a
  155. titlecase character and then a series of lowercase characters. The delimiters represent a set of characters understood
  156. to separate words. The first string character and the first non-delimiter character after a delimiter will be capitalized.
  157. Capitalization uses the Unicode title case, normally equivalent to upper case.
  158. Parameters:
  159. str - the string to capitalize fully
  160. delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
  161. Returns:
  162. capitalized string
  163. */
  164. func CapitalizeFully(str string, delimiters ...rune) string {
  165. var delimLen int
  166. if delimiters == nil {
  167. delimLen = -1
  168. } else {
  169. delimLen = len(delimiters)
  170. }
  171. if str == "" || delimLen == 0 {
  172. return str
  173. }
  174. str = strings.ToLower(str)
  175. return Capitalize(str, delimiters...)
  176. }
  177. /*
  178. Uncapitalize uncapitalizes all the whitespace separated words in a string. Only the first letter of each word is changed.
  179. The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter
  180. character after a delimiter will be uncapitalized. Whitespace is defined by unicode.IsSpace(char).
  181. Parameters:
  182. str - the string to uncapitalize fully
  183. delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
  184. Returns:
  185. uncapitalized string
  186. */
  187. func Uncapitalize(str string, delimiters ...rune) string {
  188. var delimLen int
  189. if delimiters == nil {
  190. delimLen = -1
  191. } else {
  192. delimLen = len(delimiters)
  193. }
  194. if str == "" || delimLen == 0 {
  195. return str
  196. }
  197. buffer := []rune(str)
  198. uncapitalizeNext := true // TODO Always makes capitalize/un apply to first char.
  199. for i := 0; i < len(buffer); i++ {
  200. ch := buffer[i]
  201. if isDelimiter(ch, delimiters...) {
  202. uncapitalizeNext = true
  203. } else if uncapitalizeNext {
  204. buffer[i] = unicode.ToLower(ch)
  205. uncapitalizeNext = false
  206. }
  207. }
  208. return string(buffer)
  209. }
  210. /*
  211. SwapCase swaps the case of a string using a word based algorithm.
  212. Conversion algorithm:
  213. Upper case character converts to Lower case
  214. Title case character converts to Lower case
  215. Lower case character after Whitespace or at start converts to Title case
  216. Other Lower case character converts to Upper case
  217. Whitespace is defined by unicode.IsSpace(char).
  218. Parameters:
  219. str - the string to swap case
  220. Returns:
  221. the changed string
  222. */
  223. func SwapCase(str string) string {
  224. if str == "" {
  225. return str
  226. }
  227. buffer := []rune(str)
  228. whitespace := true
  229. for i := 0; i < len(buffer); i++ {
  230. ch := buffer[i]
  231. if unicode.IsUpper(ch) {
  232. buffer[i] = unicode.ToLower(ch)
  233. whitespace = false
  234. } else if unicode.IsTitle(ch) {
  235. buffer[i] = unicode.ToLower(ch)
  236. whitespace = false
  237. } else if unicode.IsLower(ch) {
  238. if whitespace {
  239. buffer[i] = unicode.ToTitle(ch)
  240. whitespace = false
  241. } else {
  242. buffer[i] = unicode.ToUpper(ch)
  243. }
  244. } else {
  245. whitespace = unicode.IsSpace(ch)
  246. }
  247. }
  248. return string(buffer)
  249. }
  250. /*
  251. Initials extracts the initial letters from each word in the string. The first letter of the string and all first
  252. letters after the defined delimiters are returned as a new string. Their case is not changed. If the delimiters
  253. parameter is excluded, then Whitespace is used. Whitespace is defined by unicode.IsSpacea(char). An empty delimiter array returns an empty string.
  254. Parameters:
  255. str - the string to get initials from
  256. delimiters - set of characters to determine words, exclusion of this parameter means whitespace would be delimeter
  257. Returns:
  258. string of initial letters
  259. */
  260. func Initials(str string, delimiters ...rune) string {
  261. if str == "" {
  262. return str
  263. }
  264. if delimiters != nil && len(delimiters) == 0 {
  265. return ""
  266. }
  267. strLen := len(str)
  268. var buf bytes.Buffer
  269. lastWasGap := true
  270. for i := 0; i < strLen; i++ {
  271. ch := rune(str[i])
  272. if isDelimiter(ch, delimiters...) {
  273. lastWasGap = true
  274. } else if lastWasGap {
  275. buf.WriteRune(ch)
  276. lastWasGap = false
  277. }
  278. }
  279. return buf.String()
  280. }
  281. // private function (lower case func name)
  282. func isDelimiter(ch rune, delimiters ...rune) bool {
  283. if delimiters == nil {
  284. return unicode.IsSpace(ch)
  285. }
  286. for _, delimiter := range delimiters {
  287. if ch == delimiter {
  288. return true
  289. }
  290. }
  291. return false
  292. }