Last active
April 17, 2023 02:24
-
-
Save kinjalkishor/9adc6378f9ea723d6bd224ee2f183289 to your computer and use it in GitHub Desktop.
simple variadic println in dlang (nogc) & C++ without formatting using pritnf
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
For custom types struct ostrz containing [256]char array is used, which is returned from overloaded print functions. | |
// DLANG | |
module nogc_print; | |
import core.stdc.stdio; | |
/* | |
@nogc | |
void main() { | |
println("Hello World"); | |
int a = 10; | |
println("value = ", a); | |
} | |
*/ | |
// Template function to print any type using printf from C | |
@nogc void println(T...)(T args) { | |
foreach (elem; args) { | |
internal_print(elem); | |
} | |
internal_print("\n"); | |
} | |
struct ostrz { | |
char[256] data; | |
}; | |
@nogc private void internal_print(ostrz st) { | |
printf("%s", st.data.ptr); | |
} | |
@nogc private void internal_print(string st) { | |
printf("%s", st.ptr); | |
} | |
@nogc private void internal_print(int i) { | |
printf("%i", i); | |
} | |
@nogc private void internal_print(float f) { | |
printf("%f", f); | |
} | |
@nogc private void internal_print(bool b) { | |
if (b) { | |
printf("true"); | |
} else { | |
printf("false"); | |
} | |
} | |
//------------------------------------------------------------------------ | |
// CPP | |
//struct ostrz { | |
// char[256] data; | |
//}; | |
void internal_print(ostrz st) { | |
printf("%s", st.c_str()); | |
} | |
//void internal_print(std::string st) { | |
// printf("%s", st.c_str()); | |
//} | |
void internal_print(const char* str) { | |
printf("%s", str); | |
} | |
void internal_print(int i) { | |
printf("%i", i); | |
} | |
void internal_print(float f) { | |
printf("%f", f); | |
} | |
void internal_print(bool b) { | |
if (b) { | |
printf("true"); | |
} else { | |
printf("false"); | |
} | |
} | |
void print2() { | |
printf("\n"); | |
} | |
template <typename T> | |
void print2(const T& t) { | |
internal_print(t); | |
} | |
template <typename First, typename... Rest> | |
void print2(const First& first, const Rest&... rest) { | |
internal_print(first); | |
// recursive call using pack expansion syntax | |
print2(rest...); | |
printf("\n"); | |
} | |
//int a = 5; | |
//print2("hello ", a); | |
//print2(5); | |
/* | |
void println2(const char* fmt...) | |
{ | |
va_list args; | |
va_start(args, fmt); | |
while (*fmt != '\0') { | |
if (*fmt == 'd') { | |
int i = va_arg(args, int); | |
std::cout << i << '\n'; | |
} else if (*fmt == 'c') { | |
// note automatic conversion to integral type | |
int c = va_arg(args, int); | |
std::cout << static_cast<char>(c) << '\n'; | |
} else if (*fmt == 'f') { | |
double d = va_arg(args, double); | |
std::cout << d << '\n'; | |
} | |
++fmt; | |
} | |
va_end(args); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment