| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | |
| 8 | fileencoding "reasonix/internal/fileutil/encoding" |
| 9 | ) |
| 10 | |
| 11 | func readFileUTF8(path string) ([]byte, error) { |
| 12 | return fileencoding.ReadFileUTF8(path) |
| 13 | } |
| 14 | |
| 15 | func readFileUTF8Limit(path string, limit int64) ([]byte, bool, error) { |
| 16 | if limit < 0 { |
| 17 | return nil, false, fmt.Errorf("invalid read limit %d", limit) |
| 18 | } |
| 19 | f, err := os.Open(path) |
| 20 | if err != nil { |
| 21 | return nil, false, err |
| 22 | } |
| 23 | defer f.Close() |
| 24 | |
| 25 | raw, err := io.ReadAll(io.LimitReader(f, limit+1)) |
| 26 | if err != nil { |
| 27 | return nil, false, err |
| 28 | } |
| 29 | if int64(len(raw)) > limit { |
| 30 | return nil, true, nil |
| 31 | } |
| 32 | decoded := fileencoding.DecodeToUTF8(raw) |
| 33 | if int64(len(decoded)) > limit { |
| 34 | return nil, true, nil |
| 35 | } |
| 36 | return decoded, false, nil |
| 37 | } |
| 38 |