Add krk_stringFromFormat

This commit is contained in:
K. Lange 2022-08-06 12:17:32 +09:00
parent a632144c74
commit 32c1dba829
3 changed files with 13 additions and 12 deletions

View File

@ -316,3 +316,4 @@ extern int krk_parseArgs_impl(
extern int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char * fmt, va_list args);
extern int krk_pushStringBuilderFormat(struct StringBuilder * sb, const char * fmt, ...);
extern KrkValue krk_stringFromFormat(const char * fmt, ...);

View File

@ -1213,6 +1213,15 @@ int krk_pushStringBuilderFormat(struct StringBuilder * sb, const char * fmt, ...
return result;
}
KrkValue krk_stringFromFormat(const char * fmt, ...) {
struct StringBuilder sb = {0};
va_list args;
va_start(args, fmt);
int result = krk_pushStringBuilderFormatV(&sb,fmt,args);
va_end(args);
if (!result) return krk_discardStringBuilder(&sb);
return krk_finishStringBuilder(&sb);
}
_noexport
void _createAndBind_strClass(void) {

View File

@ -245,23 +245,14 @@ void krk_module_init_kuroko(void) {
if (strstr(dir,"/bin") == (dir + strlen(dir) - 4)) {
slash = strrchr(dir,'/');
if (slash) *slash = '\0';
size_t allocSize = sizeof("/lib/kuroko/") + strlen(dir);
char * out = malloc(allocSize);
size_t len = snprintf(out, allocSize, "%s/lib/kuroko/", dir);
krk_writeValueArray(AS_LIST(module_paths), OBJECT_VAL(krk_takeString(out, len)));
krk_writeValueArray(AS_LIST(module_paths), krk_stringFromFormat("%s/lib/kuroko/", dir));
} else {
size_t allocSize = sizeof("/modules/") + strlen(dir);
char * out = malloc(allocSize);
size_t len = snprintf(out, allocSize, "%s/modules/", dir);
krk_writeValueArray(AS_LIST(module_paths), OBJECT_VAL(krk_takeString(out, len)));
krk_writeValueArray(AS_LIST(module_paths), krk_stringFromFormat("%s/modules/", dir));
}
#else
char * backslash = strrchr(dir,'\\');
if (backslash) *backslash = '\0';
size_t allocSize = sizeof("\\modules\\") + strlen(dir);
char * out = malloc(allocSize);
size_t len = snprintf(out, allocSize, "%s\\modules\\", dir);
krk_writeValueArray(AS_LIST(module_paths), OBJECT_VAL(krk_takeString(out,len)));
krk_writeValueArray(AS_LIST(module_paths), krk_stringFromFormat("%s\\modules\\", dir));
#endif
free(dir);
}