Skip to content

Commit

Permalink
Add format function for storage sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Feb 16, 2025
1 parent a94b5dc commit f7fb8d1
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 43 deletions.
4 changes: 2 additions & 2 deletions std/math/fct.spice
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Imports
import "std/math/const";
import "std/type/double" as doubleType;
import "std/type/double";

// Generic type defs
type Numeric double|int|short|long;
Expand Down Expand Up @@ -45,7 +45,7 @@ public inline f<Numeric> min<Numeric>(Numeric input1, Numeric input2) {
* @return Rounding result
*/
public inline f<int> trunc(double input) {
return doubleType::toInt(input);
return toInt(input);
}

/**
Expand Down
121 changes: 80 additions & 41 deletions std/text/format.spice
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
// Imports
import "std/text/analysis";
import "std/math/fct";
import "std/type/double" as doubleTy;
import "std/type/int" as intTy;

// Constants
const int ASCII_SHIFT_OFFSET = 32;
const int MIN_LOWER_CHAR = 97;
const int MAX_LOWER_CHAR = 122;
const int MIN_UPPER_CHAR = 65;
const int MAX_UPPER_CHAR = 90;
const unsigned int ASCII_SHIFT_OFFSET = 32;
const char MIN_LOWER_CHAR = 'a';
const char MAX_LOWER_CHAR = 'z';
const char MIN_UPPER_CHAR = 'A';
const char MAX_UPPER_CHAR = 'Z';

// Generic type defs
type IntLongShort int|long|short;

/**
* Returns a formatted storage string (e.g. 1.4 MB for 1,500,000)
* Returns the given char as upper case version
*
* @return Formatted size string
* @return Char in upper case
*/
public f<String> formatStorageSize(long bytes) {
// ToDo when string concatenation works
return String("");
public f<char> toUpper(char c) {
if c >= MIN_LOWER_CHAR && c <= MAX_LOWER_CHAR {
c -= cast<char>(ASCII_SHIFT_OFFSET);
}
return c;
}

/**
* Returns the given text in caps
*
* @return Text in caps
*/
/*public p toUpper(String& text) {
foreach char& c : text {
if c >= (char) MIN_LOWER_CHAR && c <= (char) MAX_LOWER_CHAR {
c += (char) ASCII_SHIFT_OFFSET;
}
public f<String> toUpper(const String& text) {
result = text;
for unsigned long i = 0l; i < result.getLength(); i++ {
result[i] = toUpper(result[i]);
}
return text;
}*/
}

/**
* Returns the given char as upper case version
* Returns the given char as lower case version
*
* @return Char in upper case
* @return Char in lower case
*/
public f<char> toUpper(char c) {
if c >= cast<char>(MIN_LOWER_CHAR) && c <= cast<char>(MAX_LOWER_CHAR) {
public f<char> toLower(char c) {
if c >= MIN_UPPER_CHAR && c <= MAX_UPPER_CHAR {
c += cast<char>(ASCII_SHIFT_OFFSET);
}
return c;
Expand All @@ -48,35 +55,67 @@ public f<char> toUpper(char c) {
*
* @return Text in all-lower letters
*/
/*public p toLower(String& text) {
foreach char& c : text {
if c >= cast<char>(MIN_UPPER_CHAR) && c <= cast<char>(MAX_UPPER_CHAR) {
c -= cast<char>(ASCII_SHIFT_OFFSET);
}
public f<String> toLower(const String& text) {
result = text;
for unsigned long i = 0l; i < result.getLength(); i++ {
result[i] = toLower(result[i]);
}
return text;
}*/
}

/**
* Returns the given char as lower case version
* Returns the given text in capitalized form
*
* @return Char in lower case
* @return Capitalized text
*/
public f<char> toLower(char c) {
if c >= cast<char>(MIN_UPPER_CHAR) && c <= cast<char>(MAX_UPPER_CHAR) {
c -= cast<char>(ASCII_SHIFT_OFFSET);
public f<String> capitalize(const String& text) {
result = text;
if result.getLength() >= 1 {
result[0] = toUpper(result[0]);
}
return c;
}

/**
* Returns the given text in capitalized form
* Format the given number with thousands delimiter
*
* @return Capitalized text
* @param input Number
* @param delimiter Custom thousands delimiter
* @return Formatted string
*/
public f<String> formatThousandsDelimiter(int input, char delimiter = ',') {
result = intTy::toString(input);
const unsigned long n = result.getLength();
const bool isNegative = input < 0;

long insertPosition = n - 3;
long stopPosition = isNegative ? 1l : 0l;
while insertPosition > stopPosition {
result.insert(insertPosition, delimiter);
insertPosition -= 3;
}
}

/**
* Returns a formatted storage string (e.g. 1.4 MB for 1,500,000)
*
* @param bytes Number of bytes
* @param useFactor1024 Use factor 1024 instead the default factor of 1000
* @return Formatted string
*/
/*public p capitalize(String& text) {
if text[0] >= cast<char>(MIN_LOWER_CHAR) && text[0] <= cast<char>(MAX_LOWER_CHAR) {
text[0] += cast<char>(ASCII_SHIFT_OFFSET);
public f<String> formatStorageSize(unsigned long bytes, bool useFactor1024 = false) {
const string[7] storageUnits1000 = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
const string[7] storageUnits1024 = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
assert len(storageUnits1000) == len(storageUnits1024); // Sanity check

const string[7]& storageUnits = useFactor1024 ? storageUnits1024 : storageUnits1000;
const unsigned int factor = useFactor1024 ? 1024 : 1000;

double size = cast<double>(bytes);
unsigned int unitIndex = 0u;
while size >= factor && unitIndex < len(storageUnits1000) {
size /= cast<double>(factor);
unitIndex++;
}
return text;
}*/

const double sizeRounded = round(size, 2);
return doubleTy::toString(sizeRounded) + ' ' + storageUnits[unitIndex];
}
19 changes: 19 additions & 0 deletions test/test-files/std/text/format-functions/cout.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
toUpper: J
toUpper: THIS IS A TEST.
toLower: l
toLower: this is a test.
capizalize: Word
formatStorageSize: 1.000000 B
formatStorageSize: 1.000000 B
formatStorageSize: 1.230000 KB
formatStorageSize: 1.210000 KiB
formatStorageSize: 1.230000 MB
formatStorageSize: 1.180000 MiB
formatStorageSize: 1.230000 GB
formatStorageSize: 1.150000 GiB
formatStorageSize: 1.230000 TB
formatStorageSize: 1.120000 TiB
formatStorageSize: 1.230000 PB
formatStorageSize: 1.100000 PiB
formatStorageSize: 1.230000 EB
formatStorageSize: 1.070000 EiB
24 changes: 24 additions & 0 deletions test/test-files/std/text/format-functions/source.spice
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import "std/text/format";

f<int> main() {
printf("toUpper: %c\n", toUpper('j'));
printf("toUpper: %s\n", toUpper(String("This is a test.")));
printf("toLower: %c\n", toLower('L'));
printf("toLower: %s\n", toLower(String("This is a test.")));
printf("capizalize: %s\n", capitalize(String("word")));

printf("formatStorageSize: %s\n", formatStorageSize(1l));
printf("formatStorageSize: %s\n", formatStorageSize(1l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234l));
printf("formatStorageSize: %s\n", formatStorageSize(1234l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456789l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456789l, true));
}

0 comments on commit f7fb8d1

Please sign in to comment.