You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package common
  2. import (
  3. "bufio"
  4. "os"
  5. "unicode"
  6. "unicode/utf8"
  7. )
  8. // ReadFileAsInts reads all lines from the given path and returns them in a slice of ints.
  9. // If an error occurs, the function will panic.
  10. func ReadFileAsInts(path string) []int {
  11. return readIntsWithScanner(path, bufio.ScanLines)
  12. }
  13. // ReadFileAsStrings reads all lines from the given path and returns them in a slice of strings.
  14. // If an error occurs, the function will panic.
  15. func ReadFileAsStrings(path string) []string {
  16. return readStringsWithScanner(path, bufio.ScanLines)
  17. }
  18. // ReadCsvAsInts reads all data from the given path and returns an int slice
  19. // containing comma-delimited parts. If an error occurs, the function will panic.
  20. func ReadCsvAsInts(path string) []int {
  21. return readIntsWithScanner(path, scanByCommas)
  22. }
  23. // readIntsWithScanner uses a bufio.Scanner to read ints from the file at
  24. // the given path, splitting using the given bufio.SplitFunc. If an error
  25. // occurs at any point, the function will panic.
  26. func readIntsWithScanner(path string, splitFunc bufio.SplitFunc) []int {
  27. file, err := os.Open(path)
  28. if err != nil {
  29. panic(err)
  30. }
  31. defer func() {
  32. _ = file.Close()
  33. }()
  34. var parts []int
  35. scanner := bufio.NewScanner(file)
  36. scanner.Split(splitFunc)
  37. for scanner.Scan() {
  38. parts = append(parts, MustAtoi(scanner.Text()))
  39. }
  40. if scanner.Err() != nil {
  41. panic(scanner.Err())
  42. }
  43. return parts
  44. }
  45. // readStringsWithScanner uses a bufio.Scanner to read strings from the file at
  46. // the given path, splitting using the given bufio.SplitFunc. If an error
  47. // occurs at any point, the function will panic.
  48. func readStringsWithScanner(path string, splitFunc bufio.SplitFunc) []string {
  49. file, err := os.Open(path)
  50. if err != nil {
  51. panic(err)
  52. }
  53. defer func() {
  54. _ = file.Close()
  55. }()
  56. var parts []string
  57. scanner := bufio.NewScanner(file)
  58. scanner.Split(splitFunc)
  59. for scanner.Scan() {
  60. parts = append(parts, scanner.Text())
  61. }
  62. if scanner.Err() != nil {
  63. panic(scanner.Err())
  64. }
  65. return parts
  66. }
  67. // scanByCommas is a split function for a Scanner that returns each
  68. // comma-separated section of text, with surrounding spaces deleted.
  69. // The definition of space is set by unicode.IsSpace.
  70. func scanByCommas(data []byte, atEOF bool) (advance int, token []byte, err error) {
  71. // Skip leading spaces.
  72. start := 0
  73. for width := 0; start < len(data); start += width {
  74. var r rune
  75. r, width = utf8.DecodeRune(data[start:])
  76. if !unicode.IsSpace(r) {
  77. break
  78. }
  79. }
  80. // Scan until comma, marking end of word.
  81. for width, i := 0, start; i < len(data); i += width {
  82. var r rune
  83. r, width = utf8.DecodeRune(data[i:])
  84. if r == ',' || unicode.IsSpace(r) {
  85. return i + width, data[start:i], nil
  86. }
  87. }
  88. // If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
  89. if atEOF && len(data) > start {
  90. return len(data), data[start:], nil
  91. }
  92. // Request more data.
  93. return start, nil, nil
  94. }