#define _BLD_sfio 1
#endif
+#include <cgraph/strview.h>
#include <cgraph/exit.h>
#include <expr/exlib.h>
#include <expr/exop.h>
Exnode_t* actuals;
} Fmt_t;
-static bool streqn(const char *s1, const char *s2, size_t n) {
- return strlen(s2) == n && strncmp(s1, s2, n) == 0;
-}
-
/*
* printf %! extension function
*/
dp->size = sizeof(Sflong_t);
break;
}
- const char *txt = NULL;;
- size_t txt_len = 0;
+ strview_t txt = {0};
if (dp->n_str > 0)
{
- txt = dp->t_str;
- txt_len = (size_t)dp->n_str;
+ txt = (strview_t){.data = dp->t_str, .size = (size_t)dp->n_str};
}
switch (dp->fmt)
{
case 'S':
dp->flags &= ~SFFMT_LONG;
s = *(char**)vp;
- if (txt)
+ if (txt.data != NULL)
{
- if (streqn(txt, "identifier", txt_len))
+ if (strview_str_eq(txt, "identifier"))
{
if (*s && !isalpha((int)*s))
*s++ = '_';
if (!isalnum((int)*s))
*s = '_';
}
- else if (streqn(txt, "invert", txt_len))
+ else if (strview_str_eq(txt, "invert"))
{
for (; *s; s++)
if (isupper((int)*s))
else if (islower((int)*s))
*s = (char)toupper(*s);
}
- else if (streqn(txt, "lower", txt_len))
+ else if (strview_str_eq(txt, "lower"))
{
for (; *s; s++)
if (isupper((int)*s))
*s = (char)tolower(*s);
}
- else if (streqn(txt, "upper", txt_len))
+ else if (strview_str_eq(txt, "upper"))
{
for (; *s; s++)
if (islower((int)*s))
*s = (char)toupper(*s);
}
- else if (streqn(txt, "variable", txt_len))
+ else if (strview_str_eq(txt, "variable"))
{
for (; *s; s++)
if (!isalnum((int)*s) && *s != '_')
case 'T':
if ((tm = *(Sflong_t*)vp) == -1)
tm = time(NULL);
- if (!txt) {
+ if (txt.data == NULL) {
exerror("printf: no time format provided");
} else {
s = fmtbuf(TIME_LEN);
stm = localtime(&tm);
- char *format = malloc(sizeof(char) * (txt_len + 1));
+ char *format = malloc(sizeof(char) * (txt.size + 1));
if (format == NULL) {
exerror("printf: out of memory");
} else {
- strncpy(format, txt, txt_len);
- format[txt_len] = '\0';
+ strncpy(format, txt.data, txt.size);
+ format[txt.size] = '\0';
strftime(s, TIME_LEN, format, stm);
free(format);
*(char **)vp = s;