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.

csv.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package csv
  5. import (
  6. "bytes"
  7. "encoding/csv"
  8. stdcsv "encoding/csv"
  9. "errors"
  10. "io"
  11. "regexp"
  12. "strings"
  13. "code.gitea.io/gitea/modules/translation"
  14. "code.gitea.io/gitea/modules/util"
  15. )
  16. var quoteRegexp = regexp.MustCompile(`["'][\s\S]+?["']`)
  17. // CreateReader creates a csv.Reader with the given delimiter.
  18. func CreateReader(input io.Reader, delimiter rune) *stdcsv.Reader {
  19. rd := stdcsv.NewReader(input)
  20. rd.Comma = delimiter
  21. rd.TrimLeadingSpace = true
  22. return rd
  23. }
  24. // CreateReaderAndGuessDelimiter tries to guess the field delimiter from the content and creates a csv.Reader.
  25. func CreateReaderAndGuessDelimiter(rd io.Reader) (*stdcsv.Reader, error) {
  26. var data = make([]byte, 1e4)
  27. size, err := rd.Read(data)
  28. if err != nil {
  29. return nil, err
  30. }
  31. delimiter := guessDelimiter(data[:size])
  32. var newInput io.Reader
  33. if size < 1e4 {
  34. newInput = bytes.NewReader(data[:size])
  35. } else {
  36. newInput = io.MultiReader(bytes.NewReader(data), rd)
  37. }
  38. return CreateReader(newInput, delimiter), nil
  39. }
  40. // guessDelimiter scores the input CSV data against delimiters, and returns the best match.
  41. // Reads at most 10k bytes & 10 lines.
  42. func guessDelimiter(data []byte) rune {
  43. maxLines := 10
  44. maxBytes := util.Min(len(data), 1e4)
  45. text := string(data[:maxBytes])
  46. text = quoteRegexp.ReplaceAllLiteralString(text, "")
  47. lines := strings.SplitN(text, "\n", maxLines+1)
  48. lines = lines[:util.Min(maxLines, len(lines))]
  49. delimiters := []rune{',', ';', '\t', '|', '@'}
  50. bestDelim := delimiters[0]
  51. bestScore := 0.0
  52. for _, delim := range delimiters {
  53. score := scoreDelimiter(lines, delim)
  54. if score > bestScore {
  55. bestScore = score
  56. bestDelim = delim
  57. }
  58. }
  59. return bestDelim
  60. }
  61. // scoreDelimiter uses a count & regularity metric to evaluate a delimiter against lines of CSV.
  62. func scoreDelimiter(lines []string, delim rune) float64 {
  63. countTotal := 0
  64. countLineMax := 0
  65. linesNotEqual := 0
  66. for _, line := range lines {
  67. if len(line) == 0 {
  68. continue
  69. }
  70. countLine := strings.Count(line, string(delim))
  71. countTotal += countLine
  72. if countLine != countLineMax {
  73. if countLineMax != 0 {
  74. linesNotEqual++
  75. }
  76. countLineMax = util.Max(countLine, countLineMax)
  77. }
  78. }
  79. return float64(countTotal) * (1 - float64(linesNotEqual)/float64(len(lines)))
  80. }
  81. // FormatError converts csv errors into readable messages.
  82. func FormatError(err error, locale translation.Locale) (string, error) {
  83. var perr *csv.ParseError
  84. if errors.As(err, &perr) {
  85. if perr.Err == csv.ErrFieldCount {
  86. return locale.Tr("repo.error.csv.invalid_field_count", perr.Line), nil
  87. }
  88. return locale.Tr("repo.error.csv.unexpected", perr.Line, perr.Column), nil
  89. }
  90. return "", err
  91. }