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.

util.go 945B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package classic
  2. import (
  3. "time"
  4. "errors"
  5. "strings"
  6. "strconv"
  7. )
  8. // "PT6M57S" => 6 min 57 s
  9. func parseDuration(d string) (time.Duration, error) {
  10. var err error
  11. goto start
  12. error:
  13. return 0, errors.New("unknown duration code")
  14. start:
  15. if d[0:2] != "PT" { goto error }
  16. mIndex := strings.IndexByte(d, 'M')
  17. if mIndex == -1 { goto error }
  18. minutes, err := strconv.ParseUint(d[2:mIndex], 10, 32)
  19. if err != nil { return 0, err }
  20. seconds, err := strconv.ParseUint(d[mIndex:len(d)-1], 10, 32)
  21. if err != nil { return 0, err }
  22. dur := time.Duration(minutes) * time.Minute + time.Duration(seconds) * time.Second
  23. return dur, nil
  24. }
  25. // "137,802 views" => 137802
  26. func extractNumber(s string) (uint64, error) {
  27. // Extract numbers from view string
  28. var clean []byte
  29. for _, char := range []byte(s) {
  30. if char >= 0x30 && char <= 0x39 {
  31. clean = append(clean, char)
  32. }
  33. }
  34. // Convert to uint
  35. return strconv.ParseUint(string(clean), 10, 64)
  36. }