1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 19:19:53 +00:00

Begin cleaning up string API.

Remove string/pretty in favor of buffer/format and string/format. Also
drop string/number, which is more verbose and less flexible than
string/format.
This commit is contained in:
Calvin Rose 2019-02-16 15:12:34 -05:00
parent 44e31cac5d
commit 0ce5acec89
2 changed files with 6 additions and 88 deletions

View File

@ -34,7 +34,7 @@
(def buf (buffer "(" name)) (def buf (buffer "(" name))
(while (< index arglen) (while (< index arglen)
(buffer/push-string buf " ") (buffer/push-string buf " ")
(string/pretty (get args index) 4 buf) (buffer/format buf "%p" (get args index))
(set index (+ index 1))) (set index (+ index 1)))
(array/push modifiers (string buf ")\n\n" docstr)) (array/push modifiers (string buf ")\n\n" docstr))
# Build return value # Build return value
@ -1411,7 +1411,7 @@ value, one key will be ignored."
(defn pp (defn pp
"Pretty print to stdout." "Pretty print to stdout."
[x] [x]
(print (string/pretty x))) (print (buffer/format @"" "%p" x)))
### ###
### ###
@ -1718,7 +1718,7 @@ value, one key will be ignored."
(case (fiber/status f) (case (fiber/status f)
:dead (do :dead (do
(put newenv '_ @{:value x}) (put newenv '_ @{:value x})
(print (string/pretty x 20))) (print (buffer/format @"" "%.20p" x)))
(debug/stacktrace f x)))) (debug/stacktrace f x))))
(run-context {:env newenv (run-context {:env newenv
:chunks chunks :chunks chunks

View File

@ -441,66 +441,6 @@ static Janet cfun_string_join(int32_t argc, Janet *argv) {
return janet_wrap_string(janet_string_end(buf)); return janet_wrap_string(janet_string_end(buf));
} }
static struct formatter {
const char *lead;
const char *f1;
const char *f2;
} formatters[] = {
{"g", "%g", "%.*g"},
{"G", "%G", "%.*G"},
{"e", "%e", "%.*e"},
{"E", "%E", "%.*E"},
{"f", "%f", "%.*f"},
{"F", "%F", "%.*F"}
};
static Janet cfun_string_number(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 4);
double x = janet_getnumber(argv, 0);
struct formatter fmter = formatters[0];
char buf[100];
int formatNargs = 1;
int32_t precision = 0;
if (argc >= 2) {
const uint8_t *flag = janet_getkeyword(argv, 1);
int i;
for (i = 0; i < 6; i++) {
struct formatter fmttest = formatters[i];
if (!janet_cstrcmp(flag, fmttest.lead)) {
fmter = fmttest;
break;
}
}
if (i == 6)
janet_panicf("unsupported formatter %v", argv[1]);
}
if (argc >= 3) {
precision = janet_getinteger(argv, 2);
formatNargs++;
}
if (formatNargs == 1) {
snprintf(buf, sizeof(buf), fmter.f1, x);
} else if (formatNargs == 2) {
snprintf(buf, sizeof(buf), fmter.f2, precision, x);
}
return janet_cstringv(buf);
}
static Janet cfun_string_pretty(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 3);
JanetBuffer *buffer = NULL;
int32_t depth = 4;
if (argc > 1)
depth = janet_getinteger(argv, 1);
if (argc > 2)
buffer = janet_getbuffer(argv, 2);
buffer = janet_pretty(buffer, depth, argv[0]);
return janet_wrap_buffer(buffer);
}
static Janet cfun_string_format(int32_t argc, Janet *argv) { static Janet cfun_string_format(int32_t argc, Janet *argv) {
janet_arity(argc, 1, -1); janet_arity(argc, 1, -1);
JanetBuffer *buffer = janet_buffer(0); JanetBuffer *buffer = janet_buffer(0);
@ -600,32 +540,10 @@ static const JanetReg string_cfuns[] = {
"Joins an array of strings into one string, optionally separated by " "Joins an array of strings into one string, optionally separated by "
"a separator string sep.") "a separator string sep.")
}, },
{
"string/number", cfun_string_number,
JDOC("(string/number x [,format [,maxlen [,precision]]])\n\n"
"Formats a number as string. The format parameter indicates how "
"to display the number, either as floating point, scientific, or "
"whichever representation is shorter. format can be:\n\n"
"\t:g - (default) shortest representation with lowercase e.\n"
"\t:G - shortest representation with uppercase E.\n"
"\t:e - scientific with lowercase e.\n"
"\t:E - scientific with uppercase E.\n"
"\t:f - floating point representation.\n"
"\t:F - same as :f\n\n"
"The programmer can also specify the max length of the output string "
"and the precision (number of places after decimal) in the output number. "
"Returns a string representation of x.")
},
{
"string/pretty", cfun_string_pretty,
JDOC("(string/pretty x [,depth=4 [,buffer=@\"\"]])\n\n"
"Pretty prints a value to a buffer. Optionally allows setting max "
"recursion depth, as well as writing to a buffer. Returns the buffer.")
},
{ "string/format", cfun_string_format, { "string/format", cfun_string_format,
JDOC("(string/format buffer format & values)\n\n" JDOC("(string/format format & values)\n\n"
"Similar to snprintf, but specialized for operating with janet" "Similar to snprintf, but specialized for operating with janet. Returns "
"...") "a new string.")
}, },
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };