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 869B

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