Skip to content

dataunits #

Data size units converter

Installation:

v install --git https://github.com/gechandesu/dataunits

Example:

import dataunits

fn main() {
    // convert via convert fn
    kilobytes := dataunits.convert(500, dataunits.mbit, dataunits.kb)
    println(kilobytes) // 62500.0

    // convert via DataSize method (the arguments order matters)
    mebibytes := (dataunits.gib * 15).mib()
    println(mebibytes) // 15360.0

    // convert via DataSize method with explicit type cast
    bytes := dataunits.DataSize(2000 * dataunits.gib).byte()
    println(bytes) // 2.147483648e+12 == 2147483648000
}

Constants #

const bit = DataSize(1)
const nibble = bit * 4
const byte = bit * 8
const kb = byte * 1000
const mb = kb * 1000
const gb = mb * 1000
const tb = gb * 1000
const pb = tb * 1000
const eb = pb * 1000
const zb = eb * 1000
const yb = zb * 1000
const kib = byte * 1024
const mib = kib * 1024
const gib = mib * 1024
const tib = gib * 1024
const pib = tib * 1024
const eib = pib * 1024
const zib = eib * 1024
const yib = zib * 1024
const kbit = bit * 1000
const mbit = kbit * 1000
const gbit = mbit * 1000
const tbit = gbit * 1000
const pbit = tbit * 1000
const ebit = pbit * 1000
const zbit = ebit * 1000
const ybit = zbit * 1000
const kibit = bit * 1024
const mibit = kibit * 1024
const gibit = mibit * 1024
const tibit = gibit * 1024
const pibit = tibit * 1024
const eibit = pibit * 1024
const zibit = eibit * 1024
const yibit = zibit * 1024
const prefixes = {
	// Metric size (1000^N)
	'kilo':  'k'
	'mega':  'M'
	'giga':  'G'
	'tera':  'T'
	'peta':  'P'
	'exa':   'E'
	'zetta': 'Z'
	'yotta': 'Y'
	// Binary size (1024^N)
	'kibi':  'Ki'
	'mebi':  'Mi'
	'gibi':  'Gi'
	'tebi':  'Ti'
	'pebi':  'Pi'
	'exbi':  'Ei'
	'zebi':  'Zi'
	'yobi':  'Yi'
}

fn convert #

fn convert(value f64, from DataSize, to DataSize) f64

convert returns the value converted between the from and to units.

Example

assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500

fn from_string #

fn from_string(input string, params ParseParams) !DataSize

from_string parses input and returns the actual DataSize.

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.

Example

assert dataunits.from_string('GiB')! == dataunits.gib
assert dataunits.from_string('M')! == dataunits.mb
assert dataunits.from_string('M', in_bits: true)! == dataunits.mbit
assert dataunits.from_string('ZeTtAbYtEs', case_insensitive: true)! == dataunits.zb

fn to_string #

fn to_string(input DataSize) !string

to_string returns a string representation of data size unit in short form e.g. kB, Mbit, GiB, etc.

type DataSize #

type DataSize = f64

fn (DataSize) bit #

fn (d DataSize) bit() f64

fn (DataSize) nibble #

fn (d DataSize) nibble() f64

fn (DataSize) byte #

fn (d DataSize) byte() f64

fn (DataSize) kb #

fn (d DataSize) kb() f64

fn (DataSize) mb #

fn (d DataSize) mb() f64

fn (DataSize) gb #

fn (d DataSize) gb() f64

fn (DataSize) tb #

fn (d DataSize) tb() f64

fn (DataSize) pb #

fn (d DataSize) pb() f64

fn (DataSize) eb #

fn (d DataSize) eb() f64

fn (DataSize) zb #

fn (d DataSize) zb() f64

fn (DataSize) yb #

fn (d DataSize) yb() f64

fn (DataSize) kib #

fn (d DataSize) kib() f64

fn (DataSize) mib #

fn (d DataSize) mib() f64

fn (DataSize) gib #

fn (d DataSize) gib() f64

fn (DataSize) tib #

fn (d DataSize) tib() f64

fn (DataSize) pib #

fn (d DataSize) pib() f64

fn (DataSize) eib #

fn (d DataSize) eib() f64

fn (DataSize) zib #

fn (d DataSize) zib() f64

fn (DataSize) yib #

fn (d DataSize) yib() f64

fn (DataSize) kbit #

fn (d DataSize) kbit() f64

fn (DataSize) mbit #

fn (d DataSize) mbit() f64

fn (DataSize) gbit #

fn (d DataSize) gbit() f64

fn (DataSize) tbit #

fn (d DataSize) tbit() f64

fn (DataSize) pbit #

fn (d DataSize) pbit() f64

fn (DataSize) ebit #

fn (d DataSize) ebit() f64

fn (DataSize) zbit #

fn (d DataSize) zbit() f64

fn (DataSize) ybit #

fn (d DataSize) ybit() f64

fn (DataSize) kibit #

fn (d DataSize) kibit() f64

fn (DataSize) mibit #

fn (d DataSize) mibit() f64

fn (DataSize) gibit #

fn (d DataSize) gibit() f64

fn (DataSize) tibit #

fn (d DataSize) tibit() f64

fn (DataSize) pibit #

fn (d DataSize) pibit() f64

fn (DataSize) eibit #

fn (d DataSize) eibit() f64

fn (DataSize) zibit #

fn (d DataSize) zibit() f64

fn (DataSize) yibit #

fn (d DataSize) yibit() f64

struct ParseParams #

@[params]
struct ParseParams {
pub:
	// if true parse string in case insensitive mode
	case_insensitive bool
	// if true accept input such 'M', 'Gi' as bits, otherwise as bytes
	in_bits bool
	// if true accept single letter prefix as binary (power of two), otherwise as metric prefix
	binary_size bool
}