Compare commits

..

3 Commits

Author SHA1 Message Date
ge
1a0d5c8ca7 ci: update docs CI 2025-12-26 02:39:57 +03:00
ge
8e41fdc40a mod: bump version 2025-12-26 02:37:51 +03:00
ge
bc5f194a50 all: optimizations & docs fixes 2025-12-26 02:36:59 +03:00
5 changed files with 117 additions and 36 deletions

View File

@@ -21,7 +21,7 @@ jobs:
run: | run: |
v doc -f html -m . v doc -f html -m .
pushd _docs pushd _docs
ln -vs dataunits.html index.html ln -vs ${{ github.event.repository.name }}.html index.html
ls -alFh ls -alFh
popd popd

View File

@@ -1,9 +1,9 @@
## Data size units converter # Data Unit Converter
Installation: Installation:
``` ```
v install --git https://github.com/gechandesu/dataunits v install https://github.com/gechandesu/dataunits
``` ```
Example: Example:

View File

@@ -1,10 +1,10 @@
// This is free and unencumbered software released into the public domain. // This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or // Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled // distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any // binary, for any purpose, commercial or non-commercial, and by any
// means. // means.
//
// In jurisdictions that recognize copyright laws, the author or authors // In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the // of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit // software to the public domain. We make this dedication for the benefit
@@ -12,7 +12,7 @@
// successors. We intend this dedication to be an overt act of // successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this // relinquishment in perpetuity of all present and future rights to this
// software under copyright law. // software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -20,13 +20,11 @@
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <https://unlicense.org/> // For more information, please refer to <https://unlicense.org/>
module dataunits module dataunits
import maps
pub type DataSize = f64 pub type DataSize = f64
pub fn (d DataSize) bit() f64 { pub fn (d DataSize) bit() f64 {
@@ -209,7 +207,7 @@ pub const eibit = pibit * 1024
pub const zibit = eibit * 1024 pub const zibit = eibit * 1024
pub const yibit = zibit * 1024 pub const yibit = zibit * 1024
const units = { const str_unit = {
'bit': bit 'bit': bit
'nibble': nibble 'nibble': nibble
'byte': byte 'byte': byte
@@ -247,8 +245,46 @@ const units = {
'Yibit': yibit 'Yibit': yibit
} }
const unit_str = {
bit: 'bit'
nibble: 'nibble'
byte: 'byte'
kb: 'kB'
mb: 'MB'
gb: 'GB'
tb: 'TB'
pb: 'PB'
eb: 'EB'
zb: 'ZB'
yb: 'YB'
kib: 'KiB'
mib: 'MiB'
gib: 'GiB'
tib: 'TiB'
pib: 'PiB'
eib: 'EiB'
zib: 'ZiB'
yib: 'YiB'
kbit: 'kbit'
mbit: 'Mbit'
gbit: 'Gbit'
tbit: 'Tbit'
pbit: 'Pbit'
ebit: 'Ebit'
zbit: 'Zbit'
ybit: 'Ybit'
kibit: 'Kibit'
mibit: 'Mibit'
gibit: 'Gibit'
tibit: 'Tibit'
pibit: 'Pibit'
eibit: 'Eibit'
zibit: 'Zibit'
yibit: 'Yibit'
}
pub const prefixes = { pub const prefixes = {
// Metric size (1000^N) // Metric size (N * 1000)
'kilo': 'k' 'kilo': 'k'
'mega': 'M' 'mega': 'M'
'giga': 'G' 'giga': 'G'
@@ -257,7 +293,7 @@ pub const prefixes = {
'exa': 'E' 'exa': 'E'
'zetta': 'Z' 'zetta': 'Z'
'yotta': 'Y' 'yotta': 'Y'
// Binary size (1024^N) // Binary size (N * 1024)
'kibi': 'Ki' 'kibi': 'Ki'
'mebi': 'Mi' 'mebi': 'Mi'
'gibi': 'Gi' 'gibi': 'Gi'
@@ -268,9 +304,19 @@ pub const prefixes = {
'yobi': 'Yi' 'yobi': 'Yi'
} }
// vfmt off
const prefixes_array = [
'byte', 'B',
'kilo', 'k', 'mega', 'M', 'giga', 'G', 'tera', 'T',
'peta', 'P', 'exa', 'E', 'zetta', 'Z', 'yotta', 'Y',
'kibi', 'Ki', 'mebi', 'Mi', 'gibi', 'Gi', 'tebi', 'Ti',
'pebi', 'Pi', 'exbi', 'Ei', 'zebi', 'Zi', 'yobi', 'Yi'
]
// vfmt on
// convert returns the value converted between the *from* and *to* units. // convert returns the value converted between the *from* and *to* units.
// Example: // Example:
// ``` // ```v
// assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500 // assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500
// ```` // ````
pub fn convert(value f64, from DataSize, to DataSize) f64 { pub fn convert(value f64, from DataSize, to DataSize) f64 {
@@ -281,10 +327,10 @@ pub fn convert(value f64, from DataSize, to DataSize) f64 {
} }
// from_string parses input and returns the actual DataSize. // from_string parses input and returns the actual DataSize.
// Note: Case insensitivity makes unit abbreviations such as `Mb` (megabit) and `MB` (megabyte) // Note: Case insensitivity makes unit abbreviations such as 'Mb' (megabit) and 'MB' (megabyte)
// ambiguous. Use `bit` suffix for values in bits. The `b` suffix will be accepted as byte unit. // ambiguous. Use 'bit' suffix for values in bits. The 'b' suffix will be accepted as byte unit.
// Example: // Example:
// ``` // ```v
// assert dataunits.from_string('GiB')! == dataunits.gib // assert dataunits.from_string('GiB')! == dataunits.gib
// assert dataunits.from_string('M')! == dataunits.mb // assert dataunits.from_string('M')! == dataunits.mb
// assert dataunits.from_string('M', in_bits: true)! == dataunits.mbit // assert dataunits.from_string('M', in_bits: true)! == dataunits.mbit
@@ -292,20 +338,20 @@ pub fn convert(value f64, from DataSize, to DataSize) f64 {
// ``` // ```
pub fn from_string(input string, params ParseParams) !DataSize { pub fn from_string(input string, params ParseParams) !DataSize {
if !input.is_pure_ascii() { if !input.is_pure_ascii() {
return error('${input} non-ASCII characters is not allowed in data size unit') return error('${input} non-ASCII characters is not allowed in data unit')
} }
unit := parse_unit_str(input, params) unit := parse_str(input, params)
if params.case_insensitive { if params.case_insensitive {
for key, value in units { for key, value in str_unit {
if key.to_lower_ascii() == unit.to_lower_ascii() { if key.to_lower_ascii() == unit.to_lower_ascii() {
return value return value
} }
} }
} }
return units[unit] or { error('${input} is not a valid data size unit') } return str_unit[unit] or { error('${input} is not a valid data unit') }
} }
fn parse_unit_str(input string, params ParseParams) string { fn parse_str(input string, params ParseParams) string {
mut unit := '' mut unit := ''
match true { match true {
input.to_lower_ascii() in ['byte', 'bytes'] { input.to_lower_ascii() in ['byte', 'bytes'] {
@@ -352,10 +398,7 @@ fn parse_unit_str(input string, params ParseParams) string {
// prevent Gibit --> Git transform // prevent Gibit --> Git transform
return unit return unit
} }
// transform full names to short ones: megabits --> mbit, etc. unit = unit.all_before('s').replace_each(prefixes_array)
prefixes_array := maps.flat_map[string, string, string](prefixes, |k, v| [k, v])
unit = unit.replace_each(prefixes_array)
unit = unit.replace_each(['bytes', 'B', 'byte', 'B']).replace_once('bits', 'bit')
return unit return unit
} }
@@ -370,13 +413,7 @@ pub:
binary_size bool binary_size bool
} }
// to_string returns a string representation of data size unit in short form // to_string returns a string representation of data unit in short form e.g. kB, Mbit, GiB, etc.
// e.g. kB, Mbit, GiB, etc.
pub fn to_string(input DataSize) !string { pub fn to_string(input DataSize) !string {
for key, value in units { return unit_str[input] or { error('invalid input data unit') }
if value == input {
return key
}
}
return error('invalid input data size unit')
} }

View File

@@ -5,7 +5,7 @@ fn test_convert() {
assert (dataunits.bit * 8).bit() == 8 assert (dataunits.bit * 8).bit() == 8
assert (dataunits.bit * 8).byte() == 1 assert (dataunits.bit * 8).byte() == 1
assert (dataunits.gib * 10).mib() == 10240 assert (dataunits.gib * 10).mib() == 10240
assert (dataunits.gib * 5000).byte() == i64(5368709120000) assert (dataunits.gib * 5000).byte() == f64(5368709120000)
assert (dataunits.mbit * 500).kb() == 62500 assert (dataunits.mbit * 500).kb() == 62500
assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500 assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500
assert dataunits.DataSize(4000 * dataunits.gib).byte() == f64(4294967296000) assert dataunits.DataSize(4000 * dataunits.gib).byte() == f64(4294967296000)
@@ -37,3 +37,47 @@ fn test_from_string() {
assert dataunits.from_string('Ki')! == dataunits.kib assert dataunits.from_string('Ki')! == dataunits.kib
assert dataunits.from_string('Gi')! == dataunits.gib assert dataunits.from_string('Gi')! == dataunits.gib
} }
const units = [
dataunits.bit,
dataunits.nibble,
dataunits.byte,
dataunits.kb,
dataunits.mb,
dataunits.gb,
dataunits.tb,
dataunits.pb,
dataunits.eb,
dataunits.zb,
dataunits.yb,
dataunits.kib,
dataunits.mib,
dataunits.gib,
dataunits.tib,
dataunits.pib,
dataunits.eib,
dataunits.zib,
dataunits.yib,
dataunits.kbit,
dataunits.mbit,
dataunits.gbit,
dataunits.tbit,
dataunits.pbit,
dataunits.ebit,
dataunits.zbit,
dataunits.ybit,
dataunits.kibit,
dataunits.mibit,
dataunits.gibit,
dataunits.tibit,
dataunits.pibit,
dataunits.eibit,
dataunits.zibit,
dataunits.yibit,
]
fn test_from_string_to_string() {
for unit in units {
assert dataunits.from_string(dataunits.to_string(unit)!)! == unit
}
}

4
v.mod
View File

@@ -1,7 +1,7 @@
Module { Module {
name: 'dataunits' name: 'dataunits'
description: 'Data units converter' description: 'Data unit converter'
version: '0.0.2' version: '0.1.0'
license: 'Unlicense' license: 'Unlicense'
dependencies: [] dependencies: []
} }