(s string)
| 47 | } |
| 48 | |
| 49 | func ToMegabytes(s string) (int64, error) { |
| 50 | parts := bytesPattern.FindStringSubmatch(strings.TrimSpace(s)) |
| 51 | if len(parts) < 3 { |
| 52 | return 0, invalidByteQuantityError() |
| 53 | } |
| 54 | |
| 55 | value, err := strconv.ParseInt(parts[1], 10, 0) |
| 56 | if err != nil { |
| 57 | return 0, invalidByteQuantityError() |
| 58 | } |
| 59 | |
| 60 | var bytes int64 |
| 61 | unit := strings.ToUpper(parts[2]) |
| 62 | switch unit { |
| 63 | case "T": |
| 64 | bytes = value * TERABYTE |
| 65 | case "G": |
| 66 | bytes = value * GIGABYTE |
| 67 | case "M": |
| 68 | bytes = value * MEGABYTE |
| 69 | case "K": |
| 70 | bytes = value * KILOBYTE |
| 71 | } |
| 72 | |
| 73 | return bytes / MEGABYTE, nil |
| 74 | } |
| 75 | |
| 76 | var ( |
| 77 | bytesPattern = regexp.MustCompile(`(?i)^(-?\d+)([KMGT])B?$`) |
no test coverage detected