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.

io.go 2.7KB

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