tokenmap.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package analysis
  15. import (
  16. "bufio"
  17. "bytes"
  18. "io"
  19. "io/ioutil"
  20. "strings"
  21. )
  22. type TokenMap map[string]bool
  23. func NewTokenMap() TokenMap {
  24. return make(TokenMap, 0)
  25. }
  26. // LoadFile reads in a list of tokens from a text file,
  27. // one per line.
  28. // Comments are supported using `#` or `|`
  29. func (t TokenMap) LoadFile(filename string) error {
  30. data, err := ioutil.ReadFile(filename)
  31. if err != nil {
  32. return err
  33. }
  34. return t.LoadBytes(data)
  35. }
  36. // LoadBytes reads in a list of tokens from memory,
  37. // one per line.
  38. // Comments are supported using `#` or `|`
  39. func (t TokenMap) LoadBytes(data []byte) error {
  40. bytesReader := bytes.NewReader(data)
  41. bufioReader := bufio.NewReader(bytesReader)
  42. line, err := bufioReader.ReadString('\n')
  43. for err == nil {
  44. t.LoadLine(line)
  45. line, err = bufioReader.ReadString('\n')
  46. }
  47. // if the err was EOF we still need to process the last value
  48. if err == io.EOF {
  49. t.LoadLine(line)
  50. return nil
  51. }
  52. return err
  53. }
  54. func (t TokenMap) LoadLine(line string) {
  55. // find the start of a comment, if any
  56. startComment := strings.IndexAny(line, "#|")
  57. if startComment >= 0 {
  58. line = line[:startComment]
  59. }
  60. tokens := strings.Fields(line)
  61. for _, token := range tokens {
  62. t.AddToken(token)
  63. }
  64. }
  65. func (t TokenMap) AddToken(token string) {
  66. t[token] = true
  67. }