Skip to content
Myr

builtin.c

This file

Path

src/builtin.c

2,176 lines · 65.6 KiB · C

Last changed by

landed by oscabriel6b53f80

History of this file

Take it

The walk stopped after 200 commits: lines older than fa6a2ff point at it.

Fix year 2038 problem on 32-bit platforms (fix #3369) (#3407)itchynybefore adoption695830c

lines 1–3why, one step back
1#if __SIZEOF_POINTER__==4
2# define _TIME_BITS 643#endif

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 4–10why, one step back
4#ifndef __sun__
5# define _XOPEN_SOURCE6# define _XOPEN_SOURCE_EXTENDED 17#else8# define _XPG69# define __EXTENSIONS__10#endif

Define `_BSD_SOURCE` in builtin.c for OpenBSD support (#3278)itchynybefore adoption17be97a

lines 11–13why, one step back
11#ifdef __OpenBSD__
12# define _BSD_SOURCE13#endif

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 14–34why, one step back
14#include <sys/time.h>
15#include <stdlib.h>16#include <stddef.h>17#include <assert.h>18#include <ctype.h>19#include <limits.h>20#include <math.h>21#ifdef HAVE_LIBONIG22#include <oniguruma.h>23#endif24#include <string.h>25#include <time.h>26#ifdef WIN3227#include <windows.h>28#endif29#include "builtin.h"30#include "compile.h"31#include "jq_parser.h"32#include "bytecode.h"33#include "linker.h"34#include "locfile.h"

Parse the full ISO 8601 grammar in `fromdateiso8601` (#3617)itchynybefore adoptionfd25c3e

line 35why, one step back
35#include "iso8601.h"

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 36–53why, one step back
36#include "jv_unicode.h"
37#include "jv_alloc.h"38#include "jv_dtoa.h"39#include "jv_dtoa_tsd.h"40#include "jv_private.h"41#include "util.h"424344#define BINOP(name) \45static jv f_ ## name(jq_state *jq, jv input, jv a, jv b) { \46 jv_free(input); \47 return binop_ ## name(a, b); \48}49BINOPS50#undef BINOP515253static jv type_error(jv bad, const char* msg) {

Improve error message truncation with closing delimiters (#3478)itchynybefore adoption22361cf

line 54why, one step back
54 char errbuf[30];

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 55why, one step back
55 const char *badkind = jv_kind_name(jv_get_kind(bad));

Improve error message truncation with closing delimiters (#3478)itchynybefore adoption22361cf

lines 56–58why, one step back
56 jv err = jv_invalid_with_msg(jv_string_fmt(
57 "%s (%s) %s", badkind,58 jv_dump_string_trunc(bad, errbuf, sizeof(errbuf)), msg));

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 59–62why, one step back
59 return err;
60}6162static jv type_error2(jv bad1, jv bad2, const char* msg) {

Improve error message truncation with closing delimiters (#3478)itchynybefore adoption22361cf

line 63why, one step back
63 char errbuf1[30], errbuf2[30];

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 64–65why, one step back
64 const char *badkind1 = jv_kind_name(jv_get_kind(bad1));
65 const char *badkind2 = jv_kind_name(jv_get_kind(bad2));

Improve error message truncation with closing delimiters (#3478)itchynybefore adoption22361cf

lines 66–70why, one step back
66 jv err = jv_invalid_with_msg(jv_string_fmt(
67 "%s (%s) and %s (%s) %s", badkind1,68 jv_dump_string_trunc(bad1, errbuf1, sizeof(errbuf1)),69 badkind2,70 jv_dump_string_trunc(bad2, errbuf2, sizeof(errbuf2)), msg));

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 71–300why, one step back
71 return err;
72}7374static inline jv ret_error(jv bad, jv msg) {75 jv_free(bad);76 return jv_invalid_with_msg(msg);77}7879static inline jv ret_error2(jv bad1, jv bad2, jv msg) {80 jv_free(bad1);81 jv_free(bad2);82 return jv_invalid_with_msg(msg);83}8485jv binop_plus(jv a, jv b) {86 if (jv_get_kind(a) == JV_KIND_NULL) {87 jv_free(a);88 return b;89 } else if (jv_get_kind(b) == JV_KIND_NULL) {90 jv_free(b);91 return a;92 } else if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {93 jv r = jv_number(jv_number_value(a) +94 jv_number_value(b));95 jv_free(a);96 jv_free(b);97 return r;98 } else if (jv_get_kind(a) == JV_KIND_STRING && jv_get_kind(b) == JV_KIND_STRING) {99 return jv_string_concat(a, b);100 } else if (jv_get_kind(a) == JV_KIND_ARRAY && jv_get_kind(b) == JV_KIND_ARRAY) {101 return jv_array_concat(a, b);102 } else if (jv_get_kind(a) == JV_KIND_OBJECT && jv_get_kind(b) == JV_KIND_OBJECT) {103 return jv_object_merge(a, b);104 } else {105 return type_error2(a, b, "cannot be added");106 }107}108109#ifdef __APPLE__110// macOS has a bunch of libm deprecation warnings, so let's clean those up111#ifdef HAVE_TGAMMA112#define HAVE_GAMMA113#define gamma tgamma114#endif115#ifdef HAVE___EXP10116#define HAVE_EXP10117#define exp10 __exp10118#endif119#ifdef HAVE_REMAINDER120#define HAVE_DREM121#define drem remainder122#endif123124// We replace significand with our own, since there's not a rename-replacement125#ifdef HAVE_FREXP126static double __jq_significand(double x) {127 int z;128 return 2*frexp(x, &z);129}130#define HAVE_SIGNIFICAND131#define significand __jq_significand132#elif defined(HAVE_SCALBN) && defined(HAVE_ILOGB)133static double __jq_significand(double x) {134 return scalbn(x, -ilogb(x));135}136#define HAVE_SIGNIFICAND137#define significand __jq_significand138#endif139140#endif // ifdef __APPLE__141142#define LIBM_DD(name) \143static jv f_ ## name(jq_state *jq, jv input) { \144 if (jv_get_kind(input) != JV_KIND_NUMBER) { \145 return type_error(input, "number required"); \146 } \147 jv ret = jv_number(name(jv_number_value(input))); \148 jv_free(input); \149 return ret; \150}151#define LIBM_DD_NO(name) \152static jv f_ ## name(jq_state *jq, jv input) { \153 jv error = jv_string("Error: " #name "/0 not found at build time"); \154 return ret_error(input, error); \155}156157#define LIBM_DDD(name) \158static jv f_ ## name(jq_state *jq, jv input, jv a, jv b) { \159 jv_free(input); \160 if (jv_get_kind(a) != JV_KIND_NUMBER) { \161 jv_free(b); \162 return type_error(a, "number required"); \163 } \164 if (jv_get_kind(b) != JV_KIND_NUMBER) { \165 jv_free(a); \166 return type_error(b, "number required"); \167 } \168 jv ret = jv_number(name(jv_number_value(a), jv_number_value(b))); \169 jv_free(a); \170 jv_free(b); \171 return ret; \172}173#define LIBM_DDD_NO(name) \174static jv f_ ## name(jq_state *jq, jv input, jv a, jv b) { \175 jv_free(b); \176 jv error = jv_string("Error: " #name "/2 not found at build time"); \177 return ret_error2(input, a, error); \178}179180#define LIBM_DDDD(name) \181static jv f_ ## name(jq_state *jq, jv input, jv a, jv b, jv c) { \182 jv_free(input); \183 if (jv_get_kind(a) != JV_KIND_NUMBER) { \184 jv_free(b); \185 jv_free(c); \186 return type_error(a, "number required"); \187 } \188 if (jv_get_kind(b) != JV_KIND_NUMBER) { \189 jv_free(a); \190 jv_free(c); \191 return type_error(b, "number required"); \192 } \193 if (jv_get_kind(c) != JV_KIND_NUMBER) { \194 jv_free(a); \195 jv_free(b); \196 return type_error(c, "number required"); \197 } \198 jv ret = jv_number(name(jv_number_value(a), jv_number_value(b), jv_number_value(c))); \199 jv_free(a); \200 jv_free(b); \201 jv_free(c); \202 return ret; \203}204#define LIBM_DDDD_NO(name) \205static jv f_ ## name(jq_state *jq, jv input, jv a, jv b, jv c) { \206 jv_free(c); \207 jv_free(b); \208 jv error = jv_string("Error: " #name "/3 not found at build time"); \209 return ret_error2(input, a, error); \210}211212#define LIBM_DA(name, type) \213static jv f_ ## name(jq_state *jq, jv input) { \214 if (jv_get_kind(input) != JV_KIND_NUMBER) { \215 return type_error(input, "number required"); \216 } \217 type value; \218 double d = name(jv_number_value(input), &value); \219 jv ret = JV_ARRAY(jv_number(d), jv_number(value)); \220 jv_free(input); \221 return ret; \222}223#define LIBM_DA_NO(name, type) \224static jv f_ ## name(jq_state *jq, jv input) { \225 jv error = jv_string("Error: " #name "/0 not found at build time"); \226 return ret_error(input, error); \227}228229#include "libm.h"230#undef LIBM_DDDD_NO231#undef LIBM_DDD_NO232#undef LIBM_DD_NO233#undef LIBM_DA_NO234#undef LIBM_DDDD235#undef LIBM_DDD236#undef LIBM_DD237#undef LIBM_DA238239#ifdef __APPLE__240#undef gamma241#undef drem242#undef significand243#undef exp10244#endif245246static jv f_negate(jq_state *jq, jv input) {247 if (jv_get_kind(input) != JV_KIND_NUMBER) {248 return type_error(input, "cannot be negated");249 }250 jv ret = jv_number_negate(input);251 jv_free(input);252 return ret;253}254255static jv f_startswith(jq_state *jq, jv a, jv b) {256 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING)257 return ret_error2(a, b, jv_string("startswith() requires string inputs"));258 int alen = jv_string_length_bytes(jv_copy(a));259 int blen = jv_string_length_bytes(jv_copy(b));260 jv ret;261262 if (blen <= alen && memcmp(jv_string_value(a), jv_string_value(b), blen) == 0)263 ret = jv_true();264 else265 ret = jv_false();266 jv_free(a);267 jv_free(b);268 return ret;269}270271static jv f_endswith(jq_state *jq, jv a, jv b) {272 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING)273 return ret_error2(a, b, jv_string("endswith() requires string inputs"));274 const char *astr = jv_string_value(a);275 const char *bstr = jv_string_value(b);276 size_t alen = jv_string_length_bytes(jv_copy(a));277 size_t blen = jv_string_length_bytes(jv_copy(b));278 jv ret;279280 if (alen < blen ||281 memcmp(astr + (alen - blen), bstr, blen) != 0)282 ret = jv_false();283 else284 ret = jv_true();285 jv_free(a);286 jv_free(b);287 return ret;288}289290jv binop_minus(jv a, jv b) {291 if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {292 jv r = jv_number(jv_number_value(a) - jv_number_value(b));293 jv_free(a);294 jv_free(b);295 return r;296 } else if (jv_get_kind(a) == JV_KIND_ARRAY && jv_get_kind(b) == JV_KIND_ARRAY) {297 jv out = jv_array();298 jv_array_foreach(a, i, x) {299 int include = 1;300 jv_array_foreach(b, j, y) {

Guard deep structural equality and comparison recursion (#3539)Yu-Fu Fubefore adoption7122866

lines 301–309why, one step back
301 int equal = jv_equal(jv_copy(x), y);
302 if (equal < 0) {303 jv_free(out);304 jv_free(x);305 jv_free(a);306 jv_free(b);307 return jv_invalid_with_msg(jv_string("Equality check too deep"));308 }309 if (equal) {

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 310–343why, one step back
310 include = 0;
311 break;312 }313 }314 if (include)315 out = jv_array_append(out, jv_copy(x));316 jv_free(x);317 }318 jv_free(a);319 jv_free(b);320 return out;321 } else {322 return type_error2(a, b, "cannot be subtracted");323 }324}325326jv binop_multiply(jv a, jv b) {327 jv_kind ak = jv_get_kind(a);328 jv_kind bk = jv_get_kind(b);329 if (ak == JV_KIND_NUMBER && bk == JV_KIND_NUMBER) {330 jv r = jv_number(jv_number_value(a) * jv_number_value(b));331 jv_free(a);332 jv_free(b);333 return r;334 } else if ((ak == JV_KIND_STRING && bk == JV_KIND_NUMBER) ||335 (ak == JV_KIND_NUMBER && bk == JV_KIND_STRING)) {336 jv str = a;337 jv num = b;338 if (ak == JV_KIND_NUMBER) {339 str = b;340 num = a;341 }342 double d = jv_number_value(num);343 jv_free(num);

Improve performance of repeating strings (#3272)itchynybefore adoptiondc849e9

lines 344–345why, one step back
344 return jv_string_repeat(str,
345 d < 0 || isnan(d) ? -1 : d > INT_MAX ? INT_MAX : (int)d);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 346–367why, one step back
346 } else if (ak == JV_KIND_OBJECT && bk == JV_KIND_OBJECT) {
347 return jv_object_merge_recursive(a, b);348 } else {349 return type_error2(a, b, "cannot be multiplied");350 }351}352353jv binop_divide(jv a, jv b) {354 if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {355 if (jv_number_value(b) == 0.0)356 return type_error2(a, b, "cannot be divided because the divisor is zero");357 jv r = jv_number(jv_number_value(a) / jv_number_value(b));358 jv_free(a);359 jv_free(b);360 return r;361 } else if (jv_get_kind(a) == JV_KIND_STRING && jv_get_kind(b) == JV_KIND_STRING) {362 return jv_string_split(a, b);363 } else {364 return type_error2(a, b, "cannot be divided");365 }366}367

Fix off-by-one in modulo operator (#3486)Fabian Fleischerbefore adoption061fd14

line 368why, one step back
368#define dtoi(n) ((n) < INTMAX_MIN ? INTMAX_MIN : -(n) <= INTMAX_MIN ? INTMAX_MAX : (intmax_t)(n))

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 369–392why, one step back
369jv binop_mod(jv a, jv b) {
370 if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {371 double na = jv_number_value(a);372 double nb = jv_number_value(b);373 if (isnan(na) || isnan(nb)) {374 jv_free(a);375 jv_free(b);376 return jv_number(NAN);377 }378 intmax_t bi = dtoi(nb);379 if (bi == 0)380 return type_error2(a, b, "cannot be divided (remainder) because the divisor is zero");381 // Check if the divisor is -1 to avoid overflow when the dividend is INTMAX_MIN.382 jv r = jv_number(bi == -1 ? 0 : dtoi(na) % bi);383 jv_free(a);384 jv_free(b);385 return r;386 } else {387 return type_error2(a, b, "cannot be divided (remainder)");388 }389}390#undef dtoi391392jv binop_equal(jv a, jv b) {

Guard deep structural equality and comparison recursion (#3539)Yu-Fu Fubefore adoption7122866

lines 393–396why, one step back
393 int r = jv_equal(a, b);
394 if (r < 0)395 return jv_invalid_with_msg(jv_string("Equality check too deep"));396 return jv_bool(r);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 397–399why, one step back
397}
398399jv binop_notequal(jv a, jv b) {

Guard deep structural equality and comparison recursion (#3539)Yu-Fu Fubefore adoption7122866

lines 400–403why, one step back
400 int r = jv_equal(a, b);
401 if (r < 0)402 return jv_invalid_with_msg(jv_string("Equality check too deep"));403 return jv_bool(!r);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 404–414why, one step back
404}
405406enum cmp_op {407 CMP_OP_LESS,408 CMP_OP_GREATER,409 CMP_OP_LESSEQ,410 CMP_OP_GREATEREQ411};412413static jv order_cmp(jv a, jv b, enum cmp_op op) {414 int r = jv_cmp(a, b);

Guard deep structural equality and comparison recursion (#3539)Yu-Fu Fubefore adoption7122866

lines 415–416why, one step back
415 if (r == INT_MIN)
416 return jv_invalid_with_msg(jv_string("Comparison too deep"));

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 417–440why, one step back
417 return jv_bool((op == CMP_OP_LESS && r < 0) ||
418 (op == CMP_OP_LESSEQ && r <= 0) ||419 (op == CMP_OP_GREATEREQ && r >= 0) ||420 (op == CMP_OP_GREATER && r > 0));421}422423jv binop_less(jv a, jv b) {424 return order_cmp(a, b, CMP_OP_LESS);425}426427jv binop_greater(jv a, jv b) {428 return order_cmp(a, b, CMP_OP_GREATER);429}430431jv binop_lesseq(jv a, jv b) {432 return order_cmp(a, b, CMP_OP_LESSEQ);433}434435jv binop_greatereq(jv a, jv b) {436 return order_cmp(a, b, CMP_OP_GREATEREQ);437}438439static jv f_contains(jq_state *jq, jv a, jv b) {440 if (jv_get_kind(a) == jv_get_kind(b)) {

Limit the containment check depthitchynybefore adoptiond1a1256

lines 441–444why, one step back
441 int r = jv_contains(a, b);
442 if (r < 0)443 return jv_invalid_with_msg(jv_string("Containment check too deep"));444 return jv_bool(r);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 445–468why, one step back
445 } else {
446 return type_error2(a, b, "cannot have their containment checked");447 }448}449450static jv f_dump(jq_state *jq, jv input) {451 return jv_dump_string(input, 0);452}453454static jv f_json_parse(jq_state *jq, jv input) {455 if (jv_get_kind(input) != JV_KIND_STRING)456 return type_error(input, "only strings can be parsed");457 jv res = jv_parse_sized(jv_string_value(input),458 jv_string_length_bytes(jv_copy(input)));459 jv_free(input);460 return res;461}462463static jv f_tonumber(jq_state *jq, jv input) {464 if (jv_get_kind(input) == JV_KIND_NUMBER) {465 return input;466 }467 if (jv_get_kind(input) == JV_KIND_STRING) {468 const char* s = jv_string_value(input);

Fix tonumber and toboolean to reject strings with embedded null bytes (#3496)itchynybefore adoptioned3c7f2

lines 469–472why, one step back
469 int len = jv_string_length_bytes(jv_copy(input));
470 if ((size_t)len != strlen(s)) {471 return type_error(input, "cannot be parsed as a number");472 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 473–491why, one step back
473#ifdef USE_DECNUM
474 jv number = jv_number_with_literal(s);475 if (jv_get_kind(number) == JV_KIND_INVALID) {476 return type_error(input, "cannot be parsed as a number");477 }478#else479 char *end = 0;480 double d = jvp_strtod(tsd_dtoa_context_get(), s, &end);481 if (end == 0 || *end != 0) {482 return type_error(input, "cannot be parsed as a number");483 }484 jv number = jv_number(d);485#endif486 jv_free(input);487 return number;488 }489 return type_error(input, "cannot be parsed as a number");490}491

Adds "toboolean" builtin function for converting strings to booleans (#2098)Brahm Lowerbefore adoptionc350566

lines 492–497why, one step back
492static jv f_toboolean(jq_state *jq, jv input) {
493 if (jv_get_kind(input) == JV_KIND_TRUE || jv_get_kind(input) == JV_KIND_FALSE) {494 return input;495 }496 if (jv_get_kind(input) == JV_KIND_STRING) {497 const char *s = jv_string_value(input);

Fix tonumber and toboolean to reject strings with embedded null bytes (#3496)itchynybefore adoptioned3c7f2

lines 498–501why, one step back
498 int len = jv_string_length_bytes(jv_copy(input));
499 if ((size_t)len != strlen(s)) {500 return type_error(input, "cannot be parsed as a boolean");501 }

Adds "toboolean" builtin function for converting strings to booleans (#2098)Brahm Lowerbefore adoptionc350566

lines 502–512why, one step back
502 if (strcmp(s, "true") == 0) {
503 jv_free(input);504 return jv_true();505 } else if (strcmp(s, "false") == 0) {506 jv_free(input);507 return jv_false();508 }509 }510 return type_error(input, "cannot be parsed as a boolean");511}512

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 513–520why, one step back
513static jv f_length(jq_state *jq, jv input) {
514 if (jv_get_kind(input) == JV_KIND_ARRAY) {515 return jv_number(jv_array_length(input));516 } else if (jv_get_kind(input) == JV_KIND_OBJECT) {517 return jv_number(jv_object_length(input));518 } else if (jv_get_kind(input) == JV_KIND_STRING) {519 return jv_number(jv_string_length_codepoints(input));520 } else if (jv_get_kind(input) == JV_KIND_NUMBER) {

Preserve numerical precision on `length/0` (#3275)itchynybefore adoptionb7a1201

line 521why, one step back
521 jv r = jv_number_abs(input);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 522–545why, one step back
522 jv_free(input);
523 return r;524 } else if (jv_get_kind(input) == JV_KIND_NULL) {525 jv_free(input);526 return jv_number(0);527 } else {528 return type_error(input, "has no length");529 }530}531532static jv f_tostring(jq_state *jq, jv input) {533 if (jv_get_kind(input) == JV_KIND_STRING) {534 return input;535 } else {536 return jv_dump_string(input, 0);537 }538}539540static jv f_utf8bytelength(jq_state *jq, jv input) {541 if (jv_get_kind(input) != JV_KIND_STRING)542 return type_error(input, "only strings have UTF-8 byte length");543 return jv_number(jv_string_length_bytes(input));544}545

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 546–557why, one step back
546static const unsigned char URI_UNRESERVED[128] = {
547 // 1 2 3 4 5 6 7 8 9 A B C D E F548 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00549 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10550 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, // 0x20: - .551 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0x30: 0-9552 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40: A-O553 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50: P-Z _554 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60: a-o555 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, // 0x70: p-z ~556};557

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 558–561why, one step back
558#define CHARS_ALPHANUM "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
559560static const unsigned char BASE64_ENCODE_TABLE[64 + 1] = CHARS_ALPHANUM "+/";561static const unsigned char BASE64_INVALID_ENTRY = 0xFF;

Fix one-byte over-read in BASE64_DECODE_TABLE (#3547)itchynybefore adoptiona829f67

lines 562–565why, one step back
562static const unsigned char BASE64_DECODE_TABLE[256] = {
563 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,564 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,565 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 566–575why, one step back
566 62, // +
567 0xFF, 0xFF, 0xFF,568 63, // /569 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // 0-9570 0xFF, 0xFF, 0xFF,571 99, // =572 0xFF, 0xFF, 0xFF,573 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // A-Z574 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,575 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // a-z

Fix one-byte over-read in BASE64_DECODE_TABLE (#3547)itchynybefore adoptiona829f67

lines 576–584why, one step back
576 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
577 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,578 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,579 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,580 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,581 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,582 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,583 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,584 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 585–688why, one step back
585};
586587588static jv escape_string(jv input, const char* escapings) {589590 assert(jv_get_kind(input) == JV_KIND_STRING);591 const char* lookup[128] = {0};592 const char* p = escapings;593 lookup[0] = "\\0";594 while (*p) {595 lookup[(int)*p] = p+1;596 p++;597 p += strlen(p);598 p++;599 }600601 jv ret = jv_string("");602 const char* i = jv_string_value(input);603 const char* end = i + jv_string_length_bytes(jv_copy(input));604 const char* cstart;605 int c = 0;606 while ((i = jvp_utf8_next((cstart = i), end, &c))) {607 if (c < 128 && lookup[c]) {608 ret = jv_string_append_str(ret, lookup[c]);609 } else {610 ret = jv_string_append_buf(ret, cstart, i - cstart);611 }612 }613 jv_free(input);614 return ret;615616}617618static jv f_format(jq_state *jq, jv input, jv fmt) {619 if (jv_get_kind(fmt) != JV_KIND_STRING) {620 jv_free(input);621 return type_error(fmt, "is not a valid format");622 }623 const char* fmt_s = jv_string_value(fmt);624 if (!strcmp(fmt_s, "json")) {625 jv_free(fmt);626 return jv_dump_string(input, 0);627 } else if (!strcmp(fmt_s, "text")) {628 jv_free(fmt);629 return f_tostring(jq, input);630 } else if (!strcmp(fmt_s, "csv") || !strcmp(fmt_s, "tsv")) {631 const char *quotes, *sep, *escapings;632 const char *msg;633 if (!strcmp(fmt_s, "csv")) {634 msg = "cannot be csv-formatted, only array";635 quotes = "\"";636 sep = ",";637 escapings = "\"\"\"\0";638 } else {639 msg = "cannot be tsv-formatted, only array";640 assert(!strcmp(fmt_s, "tsv"));641 quotes = "";642 sep = "\t";643 escapings = "\t\\t\0\r\\r\0\n\\n\0\\\\\\\0";644 }645 jv_free(fmt);646 if (jv_get_kind(input) != JV_KIND_ARRAY)647 return type_error(input, msg);648 jv line = jv_string("");649 jv_array_foreach(input, i, x) {650 if (i) line = jv_string_append_str(line, sep);651 switch (jv_get_kind(x)) {652 case JV_KIND_NULL:653 /* null rendered as empty string */654 jv_free(x);655 break;656 case JV_KIND_TRUE:657 case JV_KIND_FALSE:658 line = jv_string_concat(line, jv_dump_string(x, 0));659 break;660 case JV_KIND_NUMBER:661 if (jv_number_value(x) != jv_number_value(x)) {662 /* NaN, render as empty string */663 jv_free(x);664 } else {665 line = jv_string_concat(line, jv_dump_string(x, 0));666 }667 break;668 case JV_KIND_STRING: {669 line = jv_string_append_str(line, quotes);670 line = jv_string_concat(line, escape_string(x, escapings));671 line = jv_string_append_str(line, quotes);672 break;673 }674 default:675 jv_free(input);676 jv_free(line);677 return type_error(x, "is not valid in a csv row");678 }679 }680 jv_free(input);681 return line;682 } else if (!strcmp(fmt_s, "html")) {683 jv_free(fmt);684 return escape_string(f_tostring(jq, input), "&&amp;\0<&lt;\0>&gt;\0'&apos;\0\"&quot;\0");685 } else if (!strcmp(fmt_s, "uri")) {686 jv_free(fmt);687 input = f_tostring(jq, input);688 const char* s = jv_string_value(input);

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 689–695why, one step back
689 int len = jv_string_length_bytes(jv_copy(input));
690 char *result = jv_mem_alloc((size_t)len * 3 + 1);691 uint32_t ri = 0;692 for (int i = 0; i < len; i++) {693 unsigned c = (unsigned)(unsigned char)s[i];694 if (c < 128 && URI_UNRESERVED[c]) {695 result[ri++] = (char)c;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 696why, one step back
696 } else {

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 697–699why, one step back
697 result[ri++] = '%';
698 result[ri++] = "0123456789ABCDEF"[c >> 4];699 result[ri++] = "0123456789ABCDEF"[c & 0x0F];

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 700–701why, one step back
700 }
701 }

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 702–703why, one step back
702 jv line = jv_string_sized(result, ri);
703 free(result);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 704–708why, one step back
704 jv_free(input);
705 return line;706 } else if (!strcmp(fmt_s, "urid")) {707 jv_free(fmt);708 input = f_tostring(jq, input);

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

line 709why, one step back
709 const char *errmsg = "is not a valid uri encoding";

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 710why, one step back
710 const char *s = jv_string_value(input);

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 711–717why, one step back
711 int len = jv_string_length_bytes(jv_copy(input));
712 char *result = jv_mem_alloc((size_t)len + 1);713 uint32_t ri = 0;714 for (int i = 0; i < len; i++) {715 char c = s[i];716 if (c != '%') {717 result[ri++] = c;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 718why, one step back
718 } else {

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 719–722why, one step back
719 int hi = 0, lo = 0;
720 for (int j = 0; j < 2; j++) {721 if (++i >= len) {722 free(result);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 723–724why, one step back
723 return type_error(input, errmsg);
724 }

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 725–732why, one step back
725 int *d = j == 0 ? &hi : &lo;
726 c = s[i];727 if ('0' <= c && c <= '9') *d = c - '0';728 else if ('a' <= c && c <= 'f') *d = c - 'a' + 10;729 else if ('A' <= c && c <= 'F') *d = c - 'A' + 10;730 else {731 free(result);732 return type_error(input, errmsg);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 733–734why, one step back
733 }
734 }

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

line 735why, one step back
735 result[ri++] = (hi << 4) | lo;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 736–737why, one step back
736 }
737 }

Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)itchynybefore adoptionbfcf82f

lines 738–743why, one step back
738 if (!jvp_utf8_is_valid(result, result + ri)) {
739 free(result);740 return type_error(input, errmsg);741 }742 jv line = jv_string_sized(result, ri);743 free(result);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 744–803why, one step back
744 jv_free(input);
745 return line;746 } else if (!strcmp(fmt_s, "sh")) {747 jv_free(fmt);748 if (jv_get_kind(input) != JV_KIND_ARRAY)749 input = jv_array_set(jv_array(), 0, input);750 jv line = jv_string("");751 jv_array_foreach(input, i, x) {752 if (i) line = jv_string_append_str(line, " ");753 switch (jv_get_kind(x)) {754 case JV_KIND_NULL:755 case JV_KIND_TRUE:756 case JV_KIND_FALSE:757 case JV_KIND_NUMBER:758 line = jv_string_concat(line, jv_dump_string(x, 0));759 break;760761 case JV_KIND_STRING: {762 line = jv_string_append_str(line, "'");763 line = jv_string_concat(line, escape_string(x, "''\\''\0"));764 line = jv_string_append_str(line, "'");765 break;766 }767768 default:769 jv_free(input);770 jv_free(line);771 return type_error(x, "can not be escaped for shell");772 }773 }774 jv_free(input);775 return line;776 } else if (!strcmp(fmt_s, "base64")) {777 jv_free(fmt);778 input = f_tostring(jq, input);779 jv line = jv_string("");780 const unsigned char* data = (const unsigned char*)jv_string_value(input);781 int len = jv_string_length_bytes(jv_copy(input));782 for (int i=0; i<len; i+=3) {783 uint32_t code = 0;784 int n = len - i >= 3 ? 3 : len-i;785 for (int j=0; j<3; j++) {786 code <<= 8;787 code |= j < n ? (unsigned)data[i+j] : 0;788 }789 char buf[4];790 for (int j=0; j<4; j++) {791 buf[j] = BASE64_ENCODE_TABLE[(code >> (18 - j*6)) & 0x3f];792 }793 if (n < 3) buf[3] = '=';794 if (n < 2) buf[2] = '=';795 line = jv_string_append_buf(line, buf, sizeof(buf));796 }797 jv_free(input);798 return line;799 } else if (!strcmp(fmt_s, "base64d")) {800 jv_free(fmt);801 input = f_tostring(jq, input);802 const unsigned char* data = (const unsigned char*)jv_string_value(input);803 int len = jv_string_length_bytes(jv_copy(input));

Fix warning: sign-compare in `@base64d` format (#3287)itchynybefore adoption668871b

line 804why, one step back
804 size_t decoded_len = MAX((3 * (size_t)len) / 4, (size_t)1); // 3 usable bytes for every 4 bytes of input

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 805–897why, one step back
805 char *result = jv_mem_calloc(decoded_len, sizeof(char));
806 uint32_t ri = 0;807 int input_bytes_read=0;808 uint32_t code = 0;809 for (int i=0; i<len && data[i] != '='; i++) {810 if (BASE64_DECODE_TABLE[data[i]] == BASE64_INVALID_ENTRY) {811 free(result);812 return type_error(input, "is not valid base64 data");813 }814815 code <<= 6;816 code |= BASE64_DECODE_TABLE[data[i]];817 input_bytes_read++;818819 if (input_bytes_read == 4) {820 result[ri++] = (code >> 16) & 0xFF;821 result[ri++] = (code >> 8) & 0xFF;822 result[ri++] = code & 0xFF;823 input_bytes_read = 0;824 code = 0;825 }826 }827 if (input_bytes_read == 3) {828 result[ri++] = (code >> 10) & 0xFF;829 result[ri++] = (code >> 2) & 0xFF;830 } else if (input_bytes_read == 2) {831 result[ri++] = (code >> 4) & 0xFF;832 } else if (input_bytes_read == 1) {833 free(result);834 return type_error(input, "trailing base64 byte found");835 }836837 jv line = jv_string_sized(result, ri);838 jv_free(input);839 free(result);840 return line;841 } else {842 jv_free(input);843 return jv_invalid_with_msg(jv_string_concat(fmt, jv_string(" is not a valid format")));844 }845}846847static jv f_keys(jq_state *jq, jv input) {848 if (jv_get_kind(input) == JV_KIND_OBJECT || jv_get_kind(input) == JV_KIND_ARRAY) {849 return jv_keys(input);850 } else {851 return type_error(input, "has no keys");852 }853}854855static jv f_keys_unsorted(jq_state *jq, jv input) {856 if (jv_get_kind(input) == JV_KIND_OBJECT || jv_get_kind(input) == JV_KIND_ARRAY) {857 return jv_keys_unsorted(input);858 } else {859 return type_error(input, "has no keys");860 }861}862863static jv f_sort(jq_state *jq, jv input){864 if (jv_get_kind(input) == JV_KIND_ARRAY) {865 return jv_sort(input, jv_copy(input));866 } else {867 return type_error(input, "cannot be sorted, as it is not an array");868 }869}870871static jv f_sort_by_impl(jq_state *jq, jv input, jv keys) {872 if (jv_get_kind(input) == JV_KIND_ARRAY &&873 jv_get_kind(keys) == JV_KIND_ARRAY &&874 jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {875 return jv_sort(input, keys);876 } else {877 return type_error2(input, keys, "cannot be sorted, as they are not both arrays");878 }879}880881/*882 * Assuming the input array is sorted, bsearch/1 returns883 * the index of the target if the target is in the input array; and otherwise884 * (-1 - ix), where ix is the insertion point that would leave the array sorted.885 * If the input is not sorted, bsearch will terminate but with irrelevant results.886 */887static jv f_bsearch(jq_state *jq, jv input, jv target) {888 if (jv_get_kind(input) != JV_KIND_ARRAY) {889 jv_free(target);890 return type_error(input, "cannot be searched from");891 }892 int start = 0;893 int end = jv_array_length(jv_copy(input));894 jv answer = jv_invalid();895 while (start < end) {896 int mid = start + (end - start) / 2;897 int result = jv_cmp(jv_copy(target), jv_array_get(jv_copy(input), mid));

Guard deep structural equality and comparison recursion (#3539)Yu-Fu Fubefore adoption7122866

lines 898–903why, one step back
898 if (result == INT_MIN) {
899 jv_free(answer);900 jv_free(input);901 jv_free(target);902 return jv_invalid_with_msg(jv_string("Comparison too deep"));903 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 904–930why, one step back
904 if (result == 0) {
905 answer = jv_number(mid);906 break;907 } else if (result < 0) {908 end = mid;909 } else {910 start = mid + 1;911 }912 }913 if (!jv_is_valid(answer)) {914 answer = jv_number(-1 - start);915 }916 jv_free(input);917 jv_free(target);918 return answer;919}920921static jv f_group_by_impl(jq_state *jq, jv input, jv keys) {922 if (jv_get_kind(input) == JV_KIND_ARRAY &&923 jv_get_kind(keys) == JV_KIND_ARRAY &&924 jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {925 return jv_group(input, keys);926 } else {927 return type_error2(input, keys, "cannot be sorted, as they are not both arrays");928 }929}930

improve performance of `unique` and `unique_by` (#3254)itchynybefore adoption95ee663

lines 931–948why, one step back
931static jv f_unique(jq_state *jq, jv input) {
932 if (jv_get_kind(input) == JV_KIND_ARRAY) {933 return jv_unique(input, jv_copy(input));934 } else {935 return type_error(input, "cannot be sorted, as it is not an array");936 }937}938939static jv f_unique_by_impl(jq_state *jq, jv input, jv keys) {940 if (jv_get_kind(input) == JV_KIND_ARRAY &&941 jv_get_kind(keys) == JV_KIND_ARRAY &&942 jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {943 return jv_unique(input, keys);944 } else {945 return type_error2(input, keys, "cannot be sorted, as they are not both arrays");946 }947}948

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 949–1089why, one step back
949#ifdef HAVE_LIBONIG
950static int f_match_name_iter(const UChar* name, const UChar *name_end, int ngroups,951 int *groups, regex_t *reg, void *arg) {952 jv captures = *(jv*)arg;953 for (int i = 0; i < ngroups; ++i) {954 jv cap = jv_array_get(jv_copy(captures),groups[i]-1);955 if (jv_get_kind(cap) == JV_KIND_OBJECT) {956 cap = jv_object_set(cap, jv_string("name"), jv_string_sized((const char*)name, name_end-name));957 captures = jv_array_set(captures,groups[i]-1,cap);958 } else {959 jv_free(cap);960 }961 }962 *(jv *)arg = captures;963 return 0;964}965966static jv f_match(jq_state *jq, jv input, jv regex, jv modifiers, jv testmode) {967 int test = jv_equal(testmode, jv_true());968 jv result;969 int onigret;970 int global = 0;971 regex_t *reg;972 OnigErrorInfo einfo;973 OnigRegion* region;974975 if (jv_get_kind(input) != JV_KIND_STRING) {976 jv_free(regex);977 jv_free(modifiers);978 return type_error(input, "cannot be matched, as it is not a string");979 }980981 if (jv_get_kind(regex) != JV_KIND_STRING) {982 jv_free(input);983 jv_free(modifiers);984 return type_error(regex, "is not a string");985 }986987 OnigOptionType options = ONIG_OPTION_CAPTURE_GROUP;988989 if (jv_get_kind(modifiers) == JV_KIND_STRING) {990 jv modarray = jv_string_explode(jv_copy(modifiers));991 jv_array_foreach(modarray, i, mod) {992 switch ((int)jv_number_value(mod)) {993 case 'g':994 global = 1;995 break;996 case 'i':997 options |= ONIG_OPTION_IGNORECASE;998 break;999 case 'x':1000 options |= ONIG_OPTION_EXTEND;1001 break;1002 case 'm':1003 options |= ONIG_OPTION_MULTILINE;1004 break;1005 case 's':1006 options |= ONIG_OPTION_SINGLELINE;1007 break;1008 case 'p':1009 options |= ONIG_OPTION_MULTILINE | ONIG_OPTION_SINGLELINE;1010 break;1011 case 'l':1012 options |= ONIG_OPTION_FIND_LONGEST;1013 break;1014 case 'n':1015 options |= ONIG_OPTION_FIND_NOT_EMPTY;1016 break;1017 default:1018 jv_free(input);1019 jv_free(regex);1020 jv_free(modarray);1021 return jv_invalid_with_msg(jv_string_concat(modifiers,1022 jv_string(" is not a valid modifier string")));1023 }1024 }1025 jv_free(modarray);1026 } else if (jv_get_kind(modifiers) != JV_KIND_NULL) {1027 // If it isn't a string or null, then it is the wrong type...1028 jv_free(input);1029 jv_free(regex);1030 return type_error(modifiers, "is not a string");1031 }10321033 jv_free(modifiers);10341035 onigret = onig_new(&reg, (const UChar*)jv_string_value(regex),1036 (const UChar*)(jv_string_value(regex) + jv_string_length_bytes(jv_copy(regex))),1037 options, ONIG_ENCODING_UTF8, ONIG_SYNTAX_PERL_NG, &einfo);1038 if (onigret != ONIG_NORMAL) {1039 UChar ebuf[ONIG_MAX_ERROR_MESSAGE_LEN];1040 onig_error_code_to_str(ebuf, onigret, &einfo);1041 jv_free(input);1042 jv_free(regex);1043 return jv_invalid_with_msg(jv_string_concat(jv_string("Regex failure: "),1044 jv_string((char*)ebuf)));1045 }1046 result = test ? jv_false() : jv_array();1047 const char *input_string = jv_string_value(input);1048 const UChar* start = (const UChar*)jv_string_value(input);1049 const unsigned long length = jv_string_length_bytes(jv_copy(input));1050 const UChar* end = start + length;1051 region = onig_region_new();1052 do {1053 onigret = onig_search(reg,1054 (const UChar*)jv_string_value(input), end, /* string boundaries */1055 start, end, /* search boundaries */1056 region, ONIG_OPTION_NONE);1057 if (onigret >= 0) {1058 if (test) {1059 result = jv_true();1060 break;1061 }10621063 // Zero-width match1064 if (region->end[0] == region->beg[0]) {1065 unsigned long idx;1066 const char *fr = (const char*)input_string;1067 for (idx = 0; fr < input_string+region->beg[0]; idx++) {1068 fr += jvp_utf8_decode_length(*fr);1069 }1070 jv match = jv_object_set(jv_object(), jv_string("offset"), jv_number(idx));1071 match = jv_object_set(match, jv_string("length"), jv_number(0));1072 match = jv_object_set(match, jv_string("string"), jv_string(""));1073 jv captures = jv_array();1074 for (int i = 1; i < region->num_regs; ++i) {1075 jv cap = jv_object();1076 if (region->beg[i] == -1) {1077 cap = jv_object_set(cap, jv_string("offset"), jv_number(-1));1078 cap = jv_object_set(cap, jv_string("string"), jv_null());1079 } else {1080 cap = jv_object_set(cap, jv_string("offset"), jv_number(idx));1081 cap = jv_object_set(cap, jv_string("string"), jv_string(""));1082 }1083 cap = jv_object_set(cap, jv_string("length"), jv_number(0));1084 cap = jv_object_set(cap, jv_string("name"), jv_null());1085 captures = jv_array_append(captures, cap);1086 }1087 onig_foreach_name(reg, f_match_name_iter, &captures);1088 match = jv_object_set(match, jv_string("captures"), captures);1089 result = jv_array_append(result, match);

Advance past the whole codepoint after a zero-width regex match (#3586)WinkleMadbefore adoptionf1f39b4

lines 1090–1097why, one step back
1090 // ensure '"qux" | match("(?=u)"; "g")' matches just once; advance the
1091 // search start past the whole codepoint, not a single byte, so we don't1092 // land in the middle of a multibyte character (which would yield a1093 // spurious duplicate match at the same offset).1094 start = (const UChar*)(input_string + region->end[0] +1095 ((size_t)region->end[0] < length1096 ? jvp_utf8_decode_length(input_string[region->end[0]])1097 : 1));

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1098–1200why, one step back
1098 continue;
1099 }11001101 unsigned long idx;1102 unsigned long len;1103 const char *fr = (const char*)input_string;11041105 for (idx = len = 0; fr < input_string+region->end[0]; len++) {1106 if (fr == input_string+region->beg[0]) idx = len, len=0;1107 fr += jvp_utf8_decode_length(*fr);1108 }11091110 jv match = jv_object_set(jv_object(), jv_string("offset"), jv_number(idx));11111112 unsigned long blen = region->end[0]-region->beg[0];1113 match = jv_object_set(match, jv_string("length"), jv_number(len));1114 match = jv_object_set(match, jv_string("string"), jv_string_sized(input_string+region->beg[0],blen));1115 jv captures = jv_array();1116 for (int i = 1; i < region->num_regs; ++i) {1117 // Empty capture.1118 if (region->beg[i] == region->end[i]) {1119 // Didn't match.1120 jv cap;1121 if (region->beg[i] == -1) {1122 cap = jv_object_set(jv_object(), jv_string("offset"), jv_number(-1));1123 cap = jv_object_set(cap, jv_string("string"), jv_null());1124 } else {1125 fr = input_string;1126 for (idx = 0; fr < input_string+region->beg[i]; idx++) {1127 fr += jvp_utf8_decode_length(*fr);1128 }1129 cap = jv_object_set(jv_object(), jv_string("offset"), jv_number(idx));1130 cap = jv_object_set(cap, jv_string("string"), jv_string(""));1131 }1132 cap = jv_object_set(cap, jv_string("length"), jv_number(0));1133 cap = jv_object_set(cap, jv_string("name"), jv_null());1134 captures = jv_array_append(captures, cap);1135 continue;1136 }1137 fr = input_string;1138 for (idx = len = 0; fr < input_string+region->end[i]; len++) {1139 if (fr == input_string+region->beg[i]) idx = len, len=0;1140 fr += jvp_utf8_decode_length(*fr);1141 }11421143 blen = region->end[i]-region->beg[i];1144 jv cap = jv_object_set(jv_object(), jv_string("offset"), jv_number(idx));1145 cap = jv_object_set(cap, jv_string("length"), jv_number(len));1146 cap = jv_object_set(cap, jv_string("string"), jv_string_sized(input_string+region->beg[i],blen));1147 cap = jv_object_set(cap, jv_string("name"), jv_null());1148 captures = jv_array_append(captures,cap);1149 }1150 onig_foreach_name(reg,f_match_name_iter,&captures);1151 match = jv_object_set(match, jv_string("captures"), captures);1152 result = jv_array_append(result, match);1153 start = (const UChar*)(input_string+region->end[0]);1154 onig_region_free(region,0);1155 } else if (onigret == ONIG_MISMATCH) {1156 break;1157 } else { /* Error */1158 UChar ebuf[ONIG_MAX_ERROR_MESSAGE_LEN];1159 onig_error_code_to_str(ebuf, onigret, &einfo);1160 jv_free(result);1161 result = jv_invalid_with_msg(jv_string_concat(jv_string("Regex failure: "),1162 jv_string((char*)ebuf)));1163 break;1164 }1165 } while (global && start <= end);1166 onig_region_free(region,1);1167 region = NULL;1168 onig_free(reg);1169 jv_free(input);1170 jv_free(regex);1171 return result;1172}1173#else /* !HAVE_LIBONIG */1174static jv f_match(jq_state *jq, jv input, jv regex, jv modifiers, jv testmode) {1175 jv_free(input);1176 jv_free(regex);1177 jv_free(modifiers);1178 jv_free(testmode);1179 return jv_invalid_with_msg(jv_string("jq was compiled without ONIGURUMA regex library. match/test/sub and related functions are not available."));1180}1181#endif /* HAVE_LIBONIG */11821183static jv minmax_by(jv values, jv keys, int is_min) {1184 if (jv_get_kind(values) != JV_KIND_ARRAY)1185 return type_error2(values, keys, "cannot be iterated over");1186 if (jv_get_kind(keys) != JV_KIND_ARRAY)1187 return type_error2(values, keys, "cannot be iterated over");1188 if (jv_array_length(jv_copy(values)) != jv_array_length(jv_copy(keys)))1189 return type_error2(values, keys, "have wrong length");11901191 if (jv_array_length(jv_copy(values)) == 0) {1192 jv_free(values);1193 jv_free(keys);1194 return jv_null();1195 }1196 jv ret = jv_array_get(jv_copy(values), 0);1197 jv retkey = jv_array_get(jv_copy(keys), 0);1198 for (int i=1; i<jv_array_length(jv_copy(values)); i++) {1199 jv item = jv_array_get(jv_copy(keys), i);1200 int cmp = jv_cmp(jv_copy(item), jv_copy(retkey));

Guard deep structural equality and comparison recursion (#3539)Yu-Fu Fubefore adoption7122866

lines 1201–1208why, one step back
1201 if (cmp == INT_MIN) {
1202 jv_free(item);1203 jv_free(values);1204 jv_free(keys);1205 jv_free(retkey);1206 jv_free(ret);1207 return jv_invalid_with_msg(jv_string("Comparison too deep"));1208 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1209–1307why, one step back
1209 if ((cmp < 0) == (is_min == 1)) {
1210 jv_free(retkey);1211 retkey = item;1212 jv_free(ret);1213 ret = jv_array_get(jv_copy(values), i);1214 } else {1215 jv_free(item);1216 }1217 }1218 jv_free(values);1219 jv_free(keys);1220 jv_free(retkey);1221 return ret;1222}12231224static jv f_min(jq_state *jq, jv x) {1225 return minmax_by(x, jv_copy(x), 1);1226}12271228static jv f_max(jq_state *jq, jv x) {1229 return minmax_by(x, jv_copy(x), 0);1230}12311232static jv f_min_by_impl(jq_state *jq, jv x, jv y) {1233 return minmax_by(x, y, 1);1234}12351236static jv f_max_by_impl(jq_state *jq, jv x, jv y) {1237 return minmax_by(x, y, 0);1238}123912401241static jv f_type(jq_state *jq, jv input) {1242 jv out = jv_string(jv_kind_name(jv_get_kind(input)));1243 jv_free(input);1244 return out;1245}12461247static jv f_isinfinite(jq_state *jq, jv input) {1248 jv_kind k = jv_get_kind(input);1249 if (k != JV_KIND_NUMBER) {1250 jv_free(input);1251 return jv_false();1252 }1253 double n = jv_number_value(input);1254 jv_free(input);1255 return isinf(n) ? jv_true() : jv_false();1256}12571258static jv f_isnan(jq_state *jq, jv input) {1259 jv_kind k = jv_get_kind(input);1260 if (k != JV_KIND_NUMBER) {1261 jv_free(input);1262 return jv_false();1263 }1264 double n = jv_number_value(input);1265 jv_free(input);1266 return isnan(n) ? jv_true() : jv_false();1267}12681269static jv f_isnormal(jq_state *jq, jv input) {1270 jv_kind k = jv_get_kind(input);1271 if (k != JV_KIND_NUMBER) {1272 jv_free(input);1273 return jv_false();1274 }1275 double n = jv_number_value(input);1276 jv_free(input);1277 return isnormal(n) ? jv_true() : jv_false();1278}12791280static jv f_infinite(jq_state *jq, jv input) {1281 jv_free(input);1282 return jv_number(INFINITY);1283}12841285static jv f_nan(jq_state *jq, jv input) {1286 jv_free(input);1287 return jv_number(NAN);1288}12891290static jv f_error(jq_state *jq, jv input) {1291 return jv_invalid_with_msg(input);1292}12931294// FIXME Should autoconf check for this!1295#ifndef WIN321296extern char **environ;1297#endif12981299static jv f_env(jq_state *jq, jv input) {1300 jv_free(input);1301 jv env = jv_object();1302 const char *var, *val;1303 for (char **e = environ; *e != NULL; e++) {1304 var = e[0];1305 val = strchr(e[0], '=');1306 if (val == NULL)1307 env = jv_object_set(env, jv_string(var), jv_null());

Fix reversed pointer subtraction in `f_env` bounds check (#3465)itchynybefore adoptionb33a763

line 1308why, one step back
1308 else if (val - var < INT_MAX)

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1309–1358why, one step back
1309 env = jv_object_set(env, jv_string_sized(var, val - var), jv_string(val + 1));
1310 }1311 return env;1312}13131314static jv f_halt(jq_state *jq, jv input) {1315 jv_free(input);1316 jq_halt(jq, jv_invalid(), jv_invalid());1317 return jv_true();1318}13191320static jv f_halt_error(jq_state *jq, jv input, jv a) {1321 if (jv_get_kind(a) != JV_KIND_NUMBER) {1322 jv_free(a);1323 return type_error(input, "halt_error/1: number required");1324 }1325 jq_halt(jq, a, input);1326 return jv_true();1327}13281329static jv f_get_search_list(jq_state *jq, jv input) {1330 jv_free(input);1331 return jq_get_lib_dirs(jq);1332}13331334static jv f_get_prog_origin(jq_state *jq, jv input) {1335 jv_free(input);1336 return jq_get_prog_origin(jq);1337}13381339static jv f_get_jq_origin(jq_state *jq, jv input) {1340 jv_free(input);1341 return jq_get_jq_origin(jq);1342}13431344static jv f_string_split(jq_state *jq, jv a, jv b) {1345 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING) {1346 return ret_error2(a, b, jv_string("split input and separator must be strings"));1347 }1348 return jv_string_split(a, b);1349}13501351static jv f_string_explode(jq_state *jq, jv a) {1352 if (jv_get_kind(a) != JV_KIND_STRING) {1353 return ret_error(a, jv_string("explode input must be a string"));1354 }1355 return jv_string_explode(a);1356}13571358static jv f_string_indexes(jq_state *jq, jv a, jv b) {

Add runtime type checks to f_string_indexestlsbolleibefore adoptionfdf8ef0

lines 1359–1366why, one step back
1359 if (jv_get_kind(a) != JV_KIND_STRING) {
1360 jv_free(b);1361 return type_error(a, "cannot be searched, as it is not a string");1362 }1363 if (jv_get_kind(b) != JV_KIND_STRING) {1364 jv_free(a);1365 return type_error(b, "is not a string");1366 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1367–1443why, one step back
1367 return jv_string_indexes(a, b);
1368}13691370enum trim_op {1371 TRIM_LEFT = 1 << 0,1372 TRIM_RIGHT = 1 << 11373};13741375static jv string_trim(jv a, int op) {1376 if (jv_get_kind(a) != JV_KIND_STRING) {1377 return ret_error(a, jv_string("trim input must be a string"));1378 }13791380 int len = jv_string_length_bytes(jv_copy(a));1381 const char *start = jv_string_value(a);1382 const char *trim_start = start;1383 const char *end = trim_start + len;1384 const char *trim_end = end;1385 int c;13861387 if (op & TRIM_LEFT) {1388 for (;;) {1389 const char *ns = jvp_utf8_next(trim_start, end, &c);1390 if (!ns || !jvp_codepoint_is_whitespace(c))1391 break;1392 trim_start = ns;1393 }1394 }13951396 // make sure not empty string or start trim has trimmed everything1397 if ((op & TRIM_RIGHT) && trim_end > trim_start) {1398 for (;;) {1399 const char *ns = jvp_utf8_backtrack(trim_end-1, trim_start, NULL);1400 jvp_utf8_next(ns, trim_end, &c);1401 if (!jvp_codepoint_is_whitespace(c))1402 break;1403 trim_end = ns;1404 if (ns == trim_start)1405 break;1406 }1407 }14081409 // no new string needed if there is nothing to trim1410 if (trim_start == start && trim_end == end)1411 return a;14121413 jv ts = jv_string_sized(trim_start, trim_end - trim_start);1414 jv_free(a);1415 return ts;1416}14171418static jv f_string_trim(jq_state *jq, jv a) { return string_trim(a, TRIM_LEFT | TRIM_RIGHT); }1419static jv f_string_ltrim(jq_state *jq, jv a) { return string_trim(a, TRIM_LEFT); }1420static jv f_string_rtrim(jq_state *jq, jv a) { return string_trim(a, TRIM_RIGHT); }14211422static jv f_string_implode(jq_state *jq, jv a) {1423 if (jv_get_kind(a) != JV_KIND_ARRAY) {1424 return ret_error(a, jv_string("implode input must be an array"));1425 }14261427 int len = jv_array_length(jv_copy(a));1428 jv s = jv_string_empty(len);14291430 for (int i = 0; i < len; i++) {1431 jv n = jv_array_get(jv_copy(a), i);1432 if (jv_get_kind(n) != JV_KIND_NUMBER || jvp_number_is_nan(n)) {1433 jv_free(a);1434 jv_free(s);1435 return type_error(n, "can't be imploded, unicode codepoint needs to be numeric");1436 }14371438 int nv = jv_number_value(n);1439 jv_free(n);1440 // outside codepoint range or in utf16 surrogate pair range1441 if (nv < 0 || nv > 0x10FFFF || (nv >= 0xD800 && nv <= 0xDFFF))1442 nv = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER1443 s = jv_string_append_codepoint(s, nv);

Tighten string length bounds and propagate invalid jv in implodeitchynybefore adoption46d1da3

line 1444why, one step back
1444 if (!jv_is_valid(s)) break;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1445–1496why, one step back
1445 }
14461447 jv_free(a);1448 return s;1449}14501451static jv f_setpath(jq_state *jq, jv a, jv b, jv c) { return jv_setpath(a, b, c); }1452extern jv _jq_path_append(jq_state *, jv, jv, jv);1453static jv f_getpath(jq_state *jq, jv a, jv b) {1454 return _jq_path_append(jq, a, b, jv_getpath(jv_copy(a), jv_copy(b)));1455}1456static jv f_delpaths(jq_state *jq, jv a, jv b) { return jv_delpaths(a, b); }1457static jv f_has(jq_state *jq, jv a, jv b) { return jv_has(a, b); }14581459static jv f_modulemeta(jq_state *jq, jv a) {1460 if (jv_get_kind(a) != JV_KIND_STRING) {1461 return ret_error(a, jv_string("modulemeta input module name must be a string"));1462 }1463 return load_module_meta(jq, a);1464}14651466static jv f_input(jq_state *jq, jv input) {1467 jv_free(input);1468 jq_input_cb cb;1469 void *data;1470 jq_get_input_cb(jq, &cb, &data);1471 if (cb == NULL)1472 return jv_invalid_with_msg(jv_string("break"));1473 jv v = cb(jq, data);1474 if (jv_is_valid(v) || jv_invalid_has_msg(jv_copy(v)))1475 return v;1476 return jv_invalid_with_msg(jv_string("break"));1477}14781479static jv f_debug(jq_state *jq, jv input) {1480 jq_msg_cb cb;1481 void *data;1482 jq_get_debug_cb(jq, &cb, &data);1483 if (cb != NULL)1484 cb(data, jv_copy(input));1485 return input;1486}14871488static jv f_stderr(jq_state *jq, jv input) {1489 jq_msg_cb cb;1490 void *data;1491 jq_get_stderr_cb(jq, &cb, &data);1492 if (cb != NULL)1493 cb(data, jv_copy(input));1494 return input;1495}1496

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1497why, one step back
1497static jv tm2jv(struct tm *tm, double fsecs) {

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1498–1502why, one step back
1498 return JV_ARRAY(jv_number(tm->tm_year + 1900),
1499 jv_number(tm->tm_mon),1500 jv_number(tm->tm_mday),1501 jv_number(tm->tm_hour),1502 jv_number(tm->tm_min),

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1503why, one step back
1503 jv_number(tm->tm_sec + (fsecs - floor(fsecs))),

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1504–1566why, one step back
1504 jv_number(tm->tm_wday),
1505 jv_number(tm->tm_yday));1506}15071508#if defined(WIN32) && !defined(HAVE_SETENV)1509static int setenv(const char *var, const char *val, int ovr)1510{1511 BOOL b;1512 char c[2];1513 if (!ovr)1514 {1515 DWORD d;1516 d = GetEnvironmentVariableA (var, c, 2);1517 if (0 != d && GetLastError () != ERROR_ENVVAR_NOT_FOUND) {1518 return d;1519 }1520 }1521 b = SetEnvironmentVariableA (var, val);1522 if (b) {1523 return 0;1524 }1525 return 1;1526}1527#endif15281529/*1530 * mktime() has side-effects and anyways, returns time in the local1531 * timezone, not UTC. We want timegm(), which isn't standard.1532 *1533 * To make things worse, mktime() tells you what the timezone1534 * adjustment is, but you have to #define _BSD_SOURCE to get this1535 * field of struct tm on some systems.1536 *1537 * This is all to blame on POSIX, of course.1538 *1539 * Our wrapper tries to use timegm() if available, or mktime() and1540 * correct for its side-effects if possible.1541 *1542 * Returns (time_t)-2 if mktime()'s side-effects cannot be corrected.1543 */1544static time_t my_mktime(struct tm *tm) {1545#ifdef HAVE_TIMEGM1546 return timegm(tm);1547#elif HAVE_TM_TM_GMT_OFF15481549 time_t t = mktime(tm);1550 if (t == (time_t)-1)1551 return t;1552 return t + tm->tm_gmtoff;1553#elif HAVE_TM___TM_GMT_OFF1554 time_t t = mktime(tm);1555 if (t == (time_t)-1)1556 return t;1557 return t + tm->__tm_gmtoff;1558#elif WIN321559 return _mkgmtime(tm);1560#else1561 char *tz;15621563 tz = (tz = getenv("TZ")) != NULL ? strdup(tz) : NULL;1564 if (tz != NULL)1565 setenv("TZ", "", 1);1566 time_t t = mktime(tm);

fix: copy the string returned by getenv (#3264)Marcin Serwinbefore adoptionf3d4c11

line 1567why, one step back
1567 if (tz != NULL) {

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1568why, one step back
1568 setenv("TZ", tz, 1);

fix: copy the string returned by getenv (#3264)Marcin Serwinbefore adoptionf3d4c11

lines 1569–1570why, one step back
1569 free(tz);
1570 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1571–1640why, one step back
1571 return t;
1572#endif1573}15741575/* Compute and set tm_wday */1576static void set_tm_wday(struct tm *tm) {1577 /*1578 * https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Gauss.27s_algorithm1579 * https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html1580 *1581 * Tested with dates from 1900-01-01 through 2100-01-01. This1582 * algorithm produces the wrong day-of-the-week number for dates in1583 * the range 1900-01-01..1900-02-28, and for 2100-01-01..2100-02-28.1584 * Since this is only needed on OS X and *BSD, we might just document1585 * this.1586 */1587 int century = (1900 + tm->tm_year) / 100;1588 int year = (1900 + tm->tm_year) % 100;1589 if (tm->tm_mon < 2)1590 year--;1591 /*1592 * The month value in the wday computation below is shifted so that1593 * March is 1, April is 2, .., January is 11, and February is 12.1594 */1595 int mon = tm->tm_mon - 1;1596 if (mon < 1)1597 mon += 12;1598 int wday =1599 (tm->tm_mday + (int)floor((2.6 * mon - 0.2)) + year + (int)floor(year / 4.0) + (int)floor(century / 4.0) - 2 * century) % 7;1600 if (wday < 0)1601 wday += 7;1602#if 01603 /* See commentary above */1604 assert(wday == tm->tm_wday || tm->tm_wday == 8);1605#endif1606 tm->tm_wday = wday;1607}1608/*1609 * Compute and set tm_yday.1610 *1611 */1612static void set_tm_yday(struct tm *tm) {1613 static const int d[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};1614 int mon = tm->tm_mon;1615 int year = 1900 + tm->tm_year;1616 int leap_day = 0;1617 if (tm->tm_mon > 1 &&1618 ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))1619 leap_day = 1;16201621 /* Bound check index into d[] */1622 if (mon < 0)1623 mon = -mon;1624 if (mon > 11)1625 mon %= 12;16261627 int yday = d[mon] + leap_day + tm->tm_mday - 1;1628 assert(yday == tm->tm_yday || tm->tm_yday == 367);1629 tm->tm_yday = yday;1630}16311632static jv f_strptime(jq_state *jq, jv a, jv b) {1633 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING) {1634 return ret_error2(a, b, jv_string("strptime/1 requires string inputs and arguments"));1635 }16361637 struct tm tm;1638 memset(&tm, 0, sizeof(tm));1639 tm.tm_wday = 8; // sentinel1640 tm.tm_yday = 367; // sentinel

Fix portability of strptime especially for Windows (#3342)itchynybefore adoption7cc202a

line 1641why, one step back
1641

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1642–1676why, one step back
1642 const char *input = jv_string_value(a);
1643 const char *fmt = jv_string_value(b);1644 const char *end = strptime(input, fmt, &tm);1645 if (end == NULL || (*end != '\0' && !isspace((unsigned char)*end))) {1646 return ret_error2(a, b, jv_string_fmt("date \"%s\" does not match format \"%s\"", input, fmt));1647 }1648 jv_free(b);1649 /*1650 * This is OS X or some *BSD whose strptime() is just not that1651 * helpful!1652 *1653 * We don't know that the format string did involve parsing a1654 * year, or a month (if tm->tm_mon == 0). But with our invalid1655 * day-of-week and day-of-year sentinel checks above, the worst1656 * this can do is produce garbage.1657 */1658#ifdef __APPLE__1659 /*1660 * Apple has made it worse, and different versions of the OS have different1661 * behaviors. Some versions just don't touch the fields, but others do, and1662 * sometimes provide wrong answers, at that! We can't tell at compile-time1663 * which behavior the target system will have, so instead we always use our1664 * functions to set these on OS X, and document that %u and %j are1665 * unsupported on OS X.1666 */1667 set_tm_wday(&tm);1668 set_tm_yday(&tm);1669#elif defined(WIN32) || !defined(HAVE_STRPTIME)1670 set_tm_wday(&tm);1671#else1672 if (tm.tm_wday == 8 && tm.tm_mday != 0 && tm.tm_mon >= 0 && tm.tm_mon <= 11)1673 set_tm_wday(&tm);1674 if (tm.tm_yday == 367 && tm.tm_mday != 0 && tm.tm_mon >= 0 && tm.tm_mon <= 11)1675 set_tm_yday(&tm);1676#endif

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1677why, one step back
1677 jv r = tm2jv(&tm, 0);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1678–1683why, one step back
1678 if (*end != '\0')
1679 r = jv_array_append(r, jv_string(end));1680 jv_free(a); // must come after `*end` because `end` is a pointer into `a`'s string1681 return r;1682}1683

Parse the full ISO 8601 grammar in `fromdateiso8601` (#3617)itchynybefore adoptionfd25c3e

lines 1684–1694why, one step back
1684static jv f_fromdateiso8601(jq_state *jq, jv a) {
1685 if (jv_get_kind(a) != JV_KIND_STRING)1686 return ret_error(a, jv_string("fromdateiso8601 requires string inputs"));1687 double t;1688 if (!iso8601_parse(jv_string_value(a), jv_string_length_bytes(jv_copy(a)), &t))1689 return ret_error(a, jv_string_fmt("date \"%s\" is not a valid ISO 8601 datetime",1690 jv_string_value(a)));1691 jv_free(a);1692 return jv_number(t);1693}1694

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

line 1695why, one step back
1695static int jv2tm(jv a, struct tm *tm, int localtime) {

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1696–1724why, one step back
1696 memset(tm, 0, sizeof(*tm));
1697 static const size_t offsets[] = {1698 offsetof(struct tm, tm_year),1699 offsetof(struct tm, tm_mon),1700 offsetof(struct tm, tm_mday),1701 offsetof(struct tm, tm_hour),1702 offsetof(struct tm, tm_min),1703 offsetof(struct tm, tm_sec),1704 offsetof(struct tm, tm_wday),1705 offsetof(struct tm, tm_yday),1706 };17071708 for (size_t i = 0; i < (sizeof offsets / sizeof *offsets); ++i) {1709 jv n = jv_array_get(jv_copy(a), i);1710 if (!jv_is_valid(n))1711 break;1712 if (jv_get_kind(n) != JV_KIND_NUMBER || jvp_number_is_nan(n)) {1713 jv_free(a);1714 jv_free(n);1715 return 0;1716 }1717 double d = jv_number_value(n);1718 if (i == 0) /* year */1719 d -= 1900;1720 *(int *)((void *)tm + offsets[i]) = d < INT_MIN ? INT_MIN :1721 d > INT_MAX ? INT_MAX : (int)d;1722 jv_free(n);1723 }1724

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

lines 1725–1738why, one step back
1725 if (localtime) {
1726 tm->tm_isdst = -1;1727 mktime(tm);1728 } else {1729#ifdef HAVE_TIMEGM1730 timegm(tm);1731#elif HAVE_TM_TM_GMT_OFF1732 // tm->tm_gmtoff = 0;1733 tm->tm_zone = "GMT";1734#elif HAVE_TM___TM_GMT_OFF1735 // tm->__tm_gmtoff = 0;1736 tm->__tm_zone = "GMT";1737#endif1738 // tm->tm_isdst = 0;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1739why, one step back
1739

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

lines 1740–1743why, one step back
1740 // The standard permits the tm structure to contain additional members. We
1741 // hope it is okay to initialize them to zero, because the standard does not1742 // provide an alternative.1743 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1744–1754why, one step back
1744
1745 jv_free(a);1746 return 1;1747}17481749#undef TO_TM_FIELD17501751static jv f_mktime(jq_state *jq, jv a) {1752 if (jv_get_kind(a) != JV_KIND_ARRAY)1753 return ret_error(a, jv_string("mktime requires array inputs"));1754 struct tm tm;

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

line 1755why, one step back
1755 if (!jv2tm(a, &tm, 0))

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1756–1771why, one step back
1756 return jv_invalid_with_msg(jv_string("mktime requires parsed datetime inputs"));
1757 time_t t = my_mktime(&tm);1758 if (t == (time_t)-1)1759 return jv_invalid_with_msg(jv_string("invalid gmtime representation"));1760 if (t == (time_t)-2)1761 return jv_invalid_with_msg(jv_string("mktime not supported on this platform"));1762 return jv_number(t);1763}17641765#ifdef HAVE_GMTIME_R1766static jv f_gmtime(jq_state *jq, jv a) {1767 if (jv_get_kind(a) != JV_KIND_NUMBER)1768 return ret_error(a, jv_string("gmtime() requires numeric inputs"));1769 struct tm tm, *tmp;1770 memset(&tm, 0, sizeof(tm));1771 double fsecs = jv_number_value(a);

Fix gmtime and localtime on negative timestamps with fractional seconds (#3634)itchynybefore adoption6aeafff

line 1772why, one step back
1772 time_t secs = floor(fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1773–1776why, one step back
1773 jv_free(a);
1774 tmp = gmtime_r(&secs, &tm);1775 if (tmp == NULL)1776 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1777why, one step back
1777 return tm2jv(tmp, fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1778–1785why, one step back
1778}
1779#elif defined HAVE_GMTIME1780static jv f_gmtime(jq_state *jq, jv a) {1781 if (jv_get_kind(a) != JV_KIND_NUMBER)1782 return ret_error(a, jv_string("gmtime requires numeric inputs"));1783 struct tm tm, *tmp;1784 memset(&tm, 0, sizeof(tm));1785 double fsecs = jv_number_value(a);

Fix gmtime and localtime on negative timestamps with fractional seconds (#3634)itchynybefore adoption6aeafff

line 1786why, one step back
1786 time_t secs = floor(fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1787–1790why, one step back
1787 jv_free(a);
1788 tmp = gmtime(&secs);1789 if (tmp == NULL)1790 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1791why, one step back
1791 return tm2jv(tmp, fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1792–1806why, one step back
1792}
1793#else1794static jv f_gmtime(jq_state *jq, jv a) {1795 jv_free(a);1796 return jv_invalid_with_msg(jv_string("gmtime not implemented on this platform"));1797}1798#endif17991800#ifdef HAVE_LOCALTIME_R1801static jv f_localtime(jq_state *jq, jv a) {1802 if (jv_get_kind(a) != JV_KIND_NUMBER)1803 return ret_error(a, jv_string("localtime() requires numeric inputs"));1804 struct tm tm, *tmp;1805 memset(&tm, 0, sizeof(tm));1806 double fsecs = jv_number_value(a);

Fix gmtime and localtime on negative timestamps with fractional seconds (#3634)itchynybefore adoption6aeafff

line 1807why, one step back
1807 time_t secs = floor(fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1808–1811why, one step back
1808 jv_free(a);
1809 tmp = localtime_r(&secs, &tm);1810 if (tmp == NULL)1811 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1812why, one step back
1812 return tm2jv(tmp, fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1813–1820why, one step back
1813}
1814#elif defined HAVE_GMTIME1815static jv f_localtime(jq_state *jq, jv a) {1816 if (jv_get_kind(a) != JV_KIND_NUMBER)1817 return ret_error(a, jv_string("localtime requires numeric inputs"));1818 struct tm tm, *tmp;1819 memset(&tm, 0, sizeof(tm));1820 double fsecs = jv_number_value(a);

Fix gmtime and localtime on negative timestamps with fractional seconds (#3634)itchynybefore adoption6aeafff

line 1821why, one step back
1821 time_t secs = floor(fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1822–1825why, one step back
1822 jv_free(a);
1823 tmp = localtime(&secs);1824 if (tmp == NULL)1825 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));

Refactor tm2jv to handle fractional seconds removing duplicated code (#3489)itchynybefore adoption601a37f

line 1826why, one step back
1826 return tm2jv(tmp, fsecs);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1827–1848why, one step back
1827}
1828#else1829static jv f_localtime(jq_state *jq, jv a) {1830 jv_free(a);1831 return jv_invalid_with_msg(jv_string("localtime not implemented on this platform"));1832}1833#endif18341835#ifdef HAVE_STRFTIME1836static jv f_strftime(jq_state *jq, jv a, jv b) {1837 if (jv_get_kind(a) == JV_KIND_NUMBER) {1838 a = f_gmtime(jq, a);1839 if (!jv_is_valid(a)) {1840 jv_free(b);1841 return a;1842 }1843 } else if (jv_get_kind(a) != JV_KIND_ARRAY) {1844 return ret_error2(a, b, jv_string("strftime/1 requires parsed datetime inputs"));1845 }1846 if (jv_get_kind(b) != JV_KIND_STRING)1847 return ret_error2(a, b, jv_string("strftime/1 requires a string format"));1848 struct tm tm;

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

line 1849why, one step back
1849 if (!jv2tm(a, &tm, 0))

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1850–1851why, one step back
1850 return ret_error(b, jv_string("strftime/1 requires parsed datetime inputs"));
18511852 /* Prepend a character so that strftime() returning 0 means the result1853 * did not fit, even if the format expands to nothing; it is trimmed below. */1854 b = jv_string_concat(jv_string(" "), b);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1855why, one step back
1855 const char *fmt = jv_string_value(b);

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

lines 1856–1857why, one step back
1856 size_t max_size = strlen(fmt) + 100;
1857 char *buf = jv_mem_alloc(max_size);

Add Solaris support (#3515)Vladimír Marekbefore adoption9f778b4

line 1858why, one step back
1858#if defined(__APPLE__) || defined(__sun)

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

lines 1859–1861why, one step back
1859 /* Apple Libc (as of version 1669.40.2) contains a bug which causes it to
1860 * ignore the `tm.tm_gmtoff` in favor of the global timezone. To print the1861 * proper timezone offset we temporarily switch the TZ to UTC. */

fix: copy the string returned by getenv (#3264)Marcin Serwinbefore adoptionf3d4c11

line 1862why, one step back
1862 char *tz = (tz = getenv("TZ")) != NULL ? strdup(tz) : NULL;

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

lines 1863–1864why, one step back
1863 setenv("TZ", "UTC", 1);
1864#endif

Add Solaris support (#3515)Vladimír Marekbefore adoption9f778b4

lines 1865–1869why, one step back
1865#if defined(__sun)
1866 /* Solaris moreover needs call to tzset to take the changed environment into1867 * account ... */1868 tzset();1869#endif

Fix strftime-related functions on NetBSD. (#3579)Thomas Klausnerbefore adoption17b4118

lines 1870–1874why, one step back
1870#ifdef __NetBSD__
1871 timezone_t tz_utc = tzalloc("UTC");1872 size_t n = strftime_z(tz_utc, buf, max_size, fmt, &tm);1873 tzfree(tz_utc);1874#else

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

line 1875why, one step back
1875 size_t n = strftime(buf, max_size, fmt, &tm);

Fix strftime-related functions on NetBSD. (#3579)Thomas Klausnerbefore adoption17b4118

line 1876why, one step back
1876#endif

Add Solaris support (#3515)Vladimír Marekbefore adoption9f778b4

line 1877why, one step back
1877#if defined(__APPLE__) || defined(__sun)

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

lines 1878–1879why, one step back
1878 if (tz) {
1879 setenv("TZ", tz, 1);

fix: copy the string returned by getenv (#3264)Marcin Serwinbefore adoptionf3d4c11

line 1880why, one step back
1880 free(tz);

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

lines 1881–1883why, one step back
1881 } else {
1882 unsetenv("TZ");1883 }

Add Solaris support (#3515)Vladimír Marekbefore adoption9f778b4

lines 1884–1886why, one step back
1884#endif
1885#if defined(__sun)1886 tzset();

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

line 1887why, one step back
1887#endif

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1888why, one step back
1888 jv_free(b);
1889 if (n == 0 || n > max_size) {

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

line 1890why, one step back
1890 free(buf);
1891 return jv_invalid_with_msg(jv_string("strftime/1: result too long"));

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

line 1892why, one step back
1892 }
1893 jv ret = jv_string_sized(buf + 1, n - 1);

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

lines 1894–1895why, one step back
1894 free(buf);
1895 return ret;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1896–1908why, one step back
1896}
1897#else1898static jv f_strftime(jq_state *jq, jv a, jv b) {1899 jv_free(a);1900 jv_free(b);1901 return jv_invalid_with_msg(jv_string("strftime/1 not implemented on this platform"));1902}1903#endif19041905#ifdef HAVE_STRFTIME1906static jv f_strflocaltime(jq_state *jq, jv a, jv b) {1907 if (jv_get_kind(a) == JV_KIND_NUMBER) {1908 a = f_localtime(jq, a);

Add missing validity check in `f_strflocaltime` after `f_localtime` (#3491)itchynybefore adoption262f0b8

lines 1909–1912why, one step back
1909 if (!jv_is_valid(a)) {
1910 jv_free(b);1911 return a;1912 }

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1913–1918why, one step back
1913 } else if (jv_get_kind(a) != JV_KIND_ARRAY) {
1914 return ret_error2(a, b, jv_string("strflocaltime/1 requires parsed datetime inputs"));1915 }1916 if (jv_get_kind(b) != JV_KIND_STRING)1917 return ret_error2(a, b, jv_string("strflocaltime/1 requires a string format"));1918 struct tm tm;

fix: populate timezone data when formatting time (#3203)Marcin Serwinbefore adoption6d8e9f9

line 1919why, one step back
1919 if (!jv2tm(a, &tm, 1))

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1920why, one step back
1920 return ret_error(b, jv_string("strflocaltime/1 requires parsed datetime inputs"));
1921 /* Prepend a character so that strftime() returning 0 means the result1922 * did not fit, even if the format expands to nothing; it is trimmed below. */1923 b = jv_string_concat(jv_string(" "), b);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1924why, one step back
1924 const char *fmt = jv_string_value(b);

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

lines 1925–1927why, one step back
1925 size_t max_size = strlen(fmt) + 100;
1926 char *buf = jv_mem_alloc(max_size);1927 size_t n = strftime(buf, max_size, fmt, &tm);

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

line 1928why, one step back
1928 jv_free(b);
1929 if (n == 0 || n > max_size) {

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

line 1930why, one step back
1930 free(buf);
1931 return jv_invalid_with_msg(jv_string("strflocaltime/1: result too long"));

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

line 1932why, one step back
1932 }
1933 jv ret = jv_string_sized(buf + 1, n - 1);

Fix segmentation fault on `strftime/1` and `strflocaltime/1` (#3271)itchynybefore adoption4003202

lines 1934–1935why, one step back
1934 free(buf);
1935 return ret;

website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff

lines 1936–2008why, one step back
1936}
1937#else1938static jv f_strflocaltime(jq_state *jq, jv a, jv b) {1939 jv_free(a);1940 jv_free(b);1941 return jv_invalid_with_msg(jv_string("strflocaltime/1 not implemented on this platform"));1942}1943#endif19441945#ifdef HAVE_GETTIMEOFDAY1946static jv f_now(jq_state *jq, jv a) {1947 jv_free(a);1948 struct timeval tv;1949 if (gettimeofday(&tv, NULL) == -1)1950 return jv_number(time(NULL));1951 return jv_number(tv.tv_sec + tv.tv_usec / 1000000.0);1952}1953#else1954static jv f_now(jq_state *jq, jv a) {1955 jv_free(a);1956 return jv_number(time(NULL));1957}1958#endif19591960static jv f_current_filename(jq_state *jq, jv a) {1961 jv_free(a);19621963 jv r = jq_util_input_get_current_filename(jq);1964 if (jv_is_valid(r))1965 return r;1966 jv_free(r);1967 return jv_null();1968}1969static jv f_current_line(jq_state *jq, jv a) {1970 jv_free(a);1971 return jq_util_input_get_current_line(jq);1972}19731974static jv f_have_decnum(jq_state *jq, jv a) {1975 jv_free(a);1976#ifdef USE_DECNUM1977 return jv_true();1978#else1979 return jv_false();1980#endif1981}19821983#define CFUNC(func, name, arity) \1984 {.fptr = { .a ## arity = func }, name, arity}19851986#define LIBM_DD(name) \1987 CFUNC(f_ ## name, #name, 1),1988#define LIBM_DD_NO(name) LIBM_DD(name)1989#define LIBM_DA(name, type) LIBM_DD(name)1990#define LIBM_DA_NO(name, type) LIBM_DD(name)19911992#define LIBM_DDD(name) \1993 CFUNC(f_ ## name, #name, 3),1994#define LIBM_DDD_NO(name) LIBM_DDD(name)19951996#define LIBM_DDDD(name) \1997 CFUNC(f_ ## name, #name, 4),1998#define LIBM_DDDD_NO(name) LIBM_DDDD(name)19992000static const struct cfunction function_list[] = {

The first 2,000 of 2,176 lines.