-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Used to make youtube-dl do random range request around a specificed chunk size
- Loading branch information
Showing
5 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package humnum | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func Atoi(s string) (int, error) { | ||
i := strings.IndexFunc(s, func(r rune) bool { | ||
return !unicode.IsDigit(r) | ||
}) | ||
if i == -1 { | ||
i = len(s) | ||
} | ||
|
||
prefix, suffix := s[0:i], s[i:] | ||
n, _ := strconv.Atoi(prefix) | ||
m := 1 | ||
if suffix != "" { | ||
switch strings.ToLower(suffix) { | ||
case "k": | ||
m = 1000 | ||
case "ki", "kibi": | ||
m = 1024 | ||
case "m": | ||
m = 1000 * 1000 | ||
case "mi", "mibi": | ||
m = 1024 * 1024 | ||
case "g": | ||
m = 1000 * 1000 * 1000 | ||
case "gi", "gibi": | ||
m = 1024 * 1024 * 1024 | ||
default: | ||
return 9, fmt.Errorf("unknown suffix %q", suffix) | ||
} | ||
} | ||
|
||
return n * m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package humnum_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/wader/ydls/internal/humnum" | ||
) | ||
|
||
func TestAtoi(t *testing.T) { | ||
testCases := []struct { | ||
s string | ||
expectedN int | ||
expectedErr string | ||
}{ | ||
{s: "", expectedN: 0}, | ||
{s: "1000", expectedN: 1000}, | ||
{s: "1K", expectedN: 1000}, | ||
{s: "1k", expectedN: 1000}, | ||
{s: "1M", expectedN: 1000_000}, | ||
{s: "1G", expectedN: 1000_000_000}, | ||
{s: "1Ki", expectedN: 1024}, | ||
{s: "1Mi", expectedN: 1024 * 1024}, | ||
{s: "1Gi", expectedN: 1024 * 1024 * 1024}, | ||
{s: "1Kibi", expectedN: 1024}, | ||
{s: "1Mibi", expectedN: 1024 * 1024}, | ||
{s: "1Gibi", expectedN: 1024 * 1024 * 1024}, | ||
{s: "123abc", expectedErr: `unknown suffix "abc"`}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(tC.s, func(t *testing.T) { | ||
actualN, actualErr := humnum.Atoi(tC.s) | ||
if (tC.expectedErr != "" && (actualErr == nil || tC.expectedErr != actualErr.Error())) && actualN != tC.expectedN { | ||
t.Errorf("expected %v (%v), got %v (%v)", tC.expectedN, tC.expectedErr, actualN, actualErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters