10 DECIMAL = 10, HEXADECIMAL = 16, BINARY = 2
18static mod_string char_to_string(
char c) {
19 mod_string str =
ralloc(2);
25static mod_string const_to_string(
string str) {
26 u_dword length = len(str);
27 mod_string mod =
ralloc(length + 1);
28 for (u_dword i = 0; i < length; i++) mod[i] = str[i];
34static char atom_to_char(u_byte b, system sys) {
35 if ((sys >= 2) && (sys <= 10))
return b + 48;
36 else if ((sys > 10) && (sys <= 36)) {
37 if (b < 10)
return b + 48;
38 else return b + 65 - 10;
39 }
else return undefined;
42static mod_string int_to_string(u_dword b, system sys) {
50 mod_string result =
ralloc(power + 1);
56 result[power] = atom_to_char(integer % sys, sys);
62static mod_string boolean_to_string(
boolean b) {
63 if (b)
return const_to_string(
"true");
64 else return const_to_string(
"false");
67static mod_string float_to_string(precise b, u_dword after_comma) {
69 mod_string integer = int_to_string((u_dword) b, DECIMAL);
70 mod_string full = concatenate(integer,
".");
73 precise floating = b - (u_dword) b;
74 for (u_dword i = 0; i < after_comma; i++) floating *= DECIMAL;
75 mod_string part = int_to_string(floating, DECIMAL);
76 mod_string result = concatenate(full, part);
81 mod_string signum = concatenate(
"-", result);
86 }
else return int_to_string((u_dword) b, DECIMAL);
89static mod_string pointer_to_string(
void* ptr) {
90 if (ptr ==
nullptr)
return const_to_string(
".null");
92 mod_string integer = int_to_string((u_dword) ptr, HEXADECIMAL);
93 mod_string result = concatenate(
".", integer);
99static mod_string number_to_string(dword num, system sys) {
101 if (num == 0) number = char_to_string(atom_to_char(0, sys));
102 else if (num < 0) number = int_to_string(-num, sys);
103 else number = int_to_string(num, sys);
111 result = concatenate(
"0x", number);
115 result = concatenate(
"0b", number);
119 mod_string prefix = format(
"(base %d) ", sys);
120 result = concatenate(prefix, number);
127 mod_string signum = concatenate(
"-", result);
136static mod_string free_concat(mod_string string1, mod_string string2) {
137 mod_string result = concatenate(string1, string2);
143mod_string format(
string template, ...) {
144 mod_string result = (mod_string)
zalloc(1);
145 u_dword argumentor = init_vararg(
sizeof(
string)), printed = 0;
146 for (
int j = 0; j < len(
template); ++j) {
147 if (
template[j] ==
'%') {
148 result = free_concat(result, substring_mid(
template, printed, j));
152 switch (
template[j]) {
154 result = free_concat(result, boolean_to_string(get_vararg(argumentor,
boolean)));
157 result = free_concat(result, pointer_to_string(get_vararg(argumentor,
void*)));
160 result = free_concat(result, number_to_string(get_vararg(argumentor, dword), DECIMAL));
163 result = free_concat(result, number_to_string(get_vararg(argumentor, dword), HEXADECIMAL));
166 result = free_concat(result, number_to_string(get_vararg(argumentor, dword), BINARY));
169 result = free_concat(result, float_to_string(get_vararg(argumentor, precise), 4));
172 result = free_concat(result, char_to_string(get_vararg(argumentor,
char)));
175 result = free_concat(result, const_to_string(get_vararg(argumentor,
string)));
185 return free_concat(result, substring_beg(
template, printed));
This is a standard MushLib heap header. See standard implementation in lib/base/heap....
void * ralloc(u_dword size)
void unalloc(void *structure)
void * zalloc(u_dword size)