"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "array.c" between
gawk-5.0.1.tar.xz and gawk-5.1.0.tar.xz

About: GNU awk - pattern scanning and processing language.

array.c  (gawk-5.0.1.tar.xz):array.c  (gawk-5.1.0.tar.xz)
/* /*
* array.c - routines for awk arrays. * array.c - routines for awk arrays.
*/ */
/* /*
* Copyright (C) 1986, 1988, 1989, 1991-2014, 2016, 2018, 2019, * Copyright (C) 1986, 1988, 1989, 1991-2014, 2016, 2018, 2019, 2020,
* the Free Software Foundation, Inc. * the Free Software Foundation, Inc.
* *
* This file is part of GAWK, the GNU implementation of the * This file is part of GAWK, the GNU implementation of the
* AWK Programming Language. * AWK Programming Language.
* *
* GAWK is free software; you can redistribute it and/or modify * GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
skipping to change at line 38 skipping to change at line 38
extern FILE *output_fp; extern FILE *output_fp;
extern NODE **fmt_list; /* declared in eval.c */ extern NODE **fmt_list; /* declared in eval.c */
NODE *success_node; NODE *success_node;
static size_t SUBSEPlen; static size_t SUBSEPlen;
static char *SUBSEP; static char *SUBSEP;
static char indent_char[] = " "; static char indent_char[] = " ";
static int sort_up_value_type(const void *p1, const void *p2);
static NODE **null_lookup(NODE *symbol, NODE *subs); static NODE **null_lookup(NODE *symbol, NODE *subs);
static NODE **null_dump(NODE *symbol, NODE *subs); static NODE **null_dump(NODE *symbol, NODE *subs);
static const array_funcs_t null_array_func = { static const array_funcs_t null_array_func = {
"null", "null",
(afunc_t) 0, (afunc_t) 0,
(afunc_t) 0, (afunc_t) 0,
null_lookup, null_lookup,
null_afunc, null_afunc,
null_afunc, null_afunc,
null_afunc, null_afunc,
skipping to change at line 764 skipping to change at line 765
* > 0, descends into 'depth' sub-arrays, and prints index and valu e info. * > 0, descends into 'depth' sub-arrays, and prints index and valu e info.
*/ */
if (nargs == 2) { if (nargs == 2) {
tmp = POP_NUMBER(); tmp = POP_NUMBER();
depth = get_number_si(tmp); depth = get_number_si(tmp);
DEREF(tmp); DEREF(tmp);
} }
symbol = POP_PARAM(); symbol = POP_PARAM();
if (symbol->type != Node_var_array) if (symbol->type != Node_var_array)
fatal(_("adump: first argument not an array")); fatal(_("%s: first argument is not an array"), "adump");
ndump.type = Node_dump_array; ndump.type = Node_dump_array;
ndump.adepth = depth; ndump.adepth = depth;
ndump.alevel = 0; ndump.alevel = 0;
assoc_dump(symbol, & ndump); assoc_dump(symbol, & ndump);
return make_number((AWKNUM) 0); return make_number((AWKNUM) 0);
} }
/* asort_actual --- do the actual work to sort the input array */ /* asort_actual --- do the actual work to sort the input array */
skipping to change at line 804 skipping to change at line 805
if (s->stlen == 0) { /* default sorting */ if (s->stlen == 0) { /* default sorting */
if (ctxt == ASORT) if (ctxt == ASORT)
sort_str = "@val_type_asc"; sort_str = "@val_type_asc";
else else
sort_str = "@ind_str_asc"; sort_str = "@ind_str_asc";
} }
if (nargs >= 2) { /* 2nd optional arg */ if (nargs >= 2) { /* 2nd optional arg */
dest = POP_PARAM(); dest = POP_PARAM();
if (dest->type != Node_var_array) { if (dest->type != Node_var_array) {
fatal(ctxt == ASORT ? fatal(_("%s: second argument is not an array"),
_("asort: second argument not an array") : ctxt == ASORT ? "asort" : "asorti");
_("asorti: second argument not an array"));
} }
} }
array = POP_PARAM(); array = POP_PARAM();
if (array->type != Node_var_array) { if (array->type != Node_var_array) {
fatal(ctxt == ASORT ? fatal(_("%s: first argument is not an array"),
_("asort: first argument not an array") : ctxt == ASORT ? "asort" : "asorti");
_("asorti: first argument not an array"));
} }
else if (array == symbol_table)
fatal(_("%s: first argument cannot be SYMTAB"),
ctxt == ASORT ? "asort" : "asorti");
else if (array == func_table)
fatal(_("%s: first argument cannot be FUNCTAB"),
ctxt == ASORT ? "asort" : "asorti");
if (dest != NULL) { if (dest != NULL) {
for (r = dest->parent_array; r != NULL; r = r->parent_array) { for (r = dest->parent_array; r != NULL; r = r->parent_array) {
if (r == array) if (r == array)
fatal(ctxt == ASORT ? fatal(_("%s: cannot use a subarray of first argum
_("asort: cannot use a subarray of first ent for second argument"),
arg for second arg") : ctxt == ASORT ? "asort" : "asorti");
_("asorti: cannot use a subarray of first
arg for second arg"));
} }
for (r = array->parent_array; r != NULL; r = r->parent_array) { for (r = array->parent_array; r != NULL; r = r->parent_array) {
if (r == dest) if (r == dest)
fatal(ctxt == ASORT ? fatal(_("%s: cannot use a subarray of second argu
_("asort: cannot use a subarray of second ment for first argument"),
arg for first arg") : ctxt == ASORT ? "asort" : "asorti");
_("asorti: cannot use a subarray of secon
d arg for first arg"));
} }
} }
/* sorting happens inside assoc_list */ /* sorting happens inside assoc_list */
list = assoc_list(array, sort_str, ctxt); list = assoc_list(array, sort_str, ctxt);
s->stptr[s->stlen] = save; s->stptr[s->stlen] = save;
DEREF(s); DEREF(s);
num_elems = assoc_length(array); num_elems = assoc_length(array);
if (num_elems == 0 || list == NULL) { if (num_elems == 0 || list == NULL) {
skipping to change at line 1053 skipping to change at line 1056
{ {
return -sort_up_index_number(p1, p2); return -sort_up_index_number(p1, p2);
} }
/* sort_up_value_string --- qsort comparison function; ascending value string */ /* sort_up_value_string --- qsort comparison function; ascending value string */
static int static int
sort_up_value_string(const void *p1, const void *p2) sort_up_value_string(const void *p1, const void *p2)
{ {
const NODE *t1, *t2; const NODE *t1, *t2;
int ret;
t1 = *((const NODE *const *) p1 + 1); t1 = *((const NODE *const *) p1 + 1);
t2 = *((const NODE *const *) p2 + 1); t2 = *((const NODE *const *) p2 + 1);
if (t1->type == Node_var_array) { if (t1->type != Node_val || t2->type != Node_val)
/* return 0 if t2 is a sub-array too, else return 1 */ return sort_up_value_type(p1, p2);
return (t2->type != Node_var_array);
}
if (t2->type == Node_var_array)
return -1; /* t1 (scalar) < t2 (sub-array) */
/* t1 and t2 both have string values */ /* t1 and t2 both have string values */
return cmp_strings(t1, t2); ret = cmp_strings(t1, t2);
if (ret != 0)
return ret;
return sort_up_index_string(p1, p2);
} }
/* sort_down_value_string --- qsort comparison function; descending value string */ /* sort_down_value_string --- qsort comparison function; descending value string */
static int static int
sort_down_value_string(const void *p1, const void *p2) sort_down_value_string(const void *p1, const void *p2)
{ {
return -sort_up_value_string(p1, p2); return -sort_up_value_string(p1, p2);
} }
skipping to change at line 1087 skipping to change at line 1090
static int static int
sort_up_value_number(const void *p1, const void *p2) sort_up_value_number(const void *p1, const void *p2)
{ {
NODE *t1, *t2; NODE *t1, *t2;
int ret; int ret;
t1 = *((NODE *const *) p1 + 1); t1 = *((NODE *const *) p1 + 1);
t2 = *((NODE *const *) p2 + 1); t2 = *((NODE *const *) p2 + 1);
if (t1->type == Node_var_array) { if (t1->type != Node_val || t2->type != Node_val)
/* return 0 if t2 is a sub-array too, else return 1 */ return sort_up_value_type(p1, p2);
return (t2->type != Node_var_array);
}
if (t2->type == Node_var_array)
return -1; /* t1 (scalar) < t2 (sub-array) */
ret = cmp_numbers(t1, t2); ret = cmp_numbers(t1, t2);
if (ret != 0) if (ret != 0)
return ret; return ret;
/* /*
* Use string value to guarantee same sort order on all * Use string value to guarantee same sort order on all
* versions of qsort(). * versions of qsort().
*/ */
t1 = force_string(t1); ret = cmp_strings(force_string(t1), force_string(t2));
t2 = force_string(t2); if (ret != 0)
return cmp_strings(t1, t2); return ret;
return sort_up_index_string(p1, p2);
} }
/* sort_down_value_number --- qsort comparison function; descending value number */ /* sort_down_value_number --- qsort comparison function; descending value number */
static int static int
sort_down_value_number(const void *p1, const void *p2) sort_down_value_number(const void *p1, const void *p2)
{ {
return -sort_up_value_number(p1, p2); return -sort_up_value_number(p1, p2);
} }
/* sort_up_value_type --- qsort comparison function; ascending value type */ /* do_sort_up_value_type --- backend comparison on ascending value type */
static int static int
sort_up_value_type(const void *p1, const void *p2) do_sort_up_value_type(const void *p1, const void *p2)
{ {
NODE *n1, *n2; NODE *n1, *n2;
static const NODETYPE element_types[] = {
Node_builtin_func,
Node_func,
Node_ext_func,
Node_var_new,
Node_var,
Node_var_array,
Node_val,
Node_illegal
};
/* we want to compare the element values */ /* we want to compare the element values */
n1 = *((NODE *const *) p1 + 1); n1 = *((NODE *const *) p1 + 1);
n2 = *((NODE *const *) p2 + 1); n2 = *((NODE *const *) p2 + 1);
/* 1. Arrays vs. scalar, scalar is less than array */ if (n1->type == Node_var && n2->type == Node_var) {
/* compare the values of the variables */
n1 = n1->var_value;
n2 = n2->var_value;
}
/* 1. Arrays vs. everything else, everything else is less than array */
if (n1->type == Node_var_array) { if (n1->type == Node_var_array) {
/* return 0 if n2 is a sub-array too, else return 1 */ /* return 0 if n2 is a sub-array too, else return 1 */
return (n2->type != Node_var_array); return (n2->type != Node_var_array);
} }
if (n2->type == Node_var_array) { if (n2->type == Node_var_array) {
return -1; /* n1 (scalar) < n2 (sub-array) */ return -1; /* n1 (non-array) < n2 (sub-array) */
}
/* 2. Non scalars */
if (n1->type != Node_val || n2->type != Node_val) {
int n1_pos, n2_pos, i;
n1_pos = n2_pos = -1;
for (i = 0; element_types[i] != Node_illegal; i++) {
if (n1->type == element_types[i])
n1_pos = i;
if (n2->type == element_types[i])
n2_pos = i;
}
assert(n1_pos != -1 && n2_pos != -1);
return (n1_pos - n2_pos);
} }
/* two scalars */ /* two scalars */
(void) fixtype(n1); (void) fixtype(n1);
(void) fixtype(n2); (void) fixtype(n2);
if ((n1->flags & NUMBER) != 0 && (n2->flags & NUMBER) != 0) { if ((n1->flags & NUMBER) != 0 && (n2->flags & NUMBER) != 0) {
return cmp_numbers(n1, n2); return cmp_numbers(n1, n2);
} }
skipping to change at line 1154 skipping to change at line 1188
if ((n1->flags & NUMBER) != 0 && (n2->flags & STRING) != 0) { if ((n1->flags & NUMBER) != 0 && (n2->flags & STRING) != 0) {
return -1; return -1;
} else if ((n1->flags & STRING) != 0 && (n2->flags & NUMBER) != 0) { } else if ((n1->flags & STRING) != 0 && (n2->flags & NUMBER) != 0) {
return 1; return 1;
} }
/* 4. Two strings */ /* 4. Two strings */
return cmp_strings(n1, n2); return cmp_strings(n1, n2);
} }
/* sort_up_value_type --- qsort comparison function; ascending value type */
static int
sort_up_value_type(const void *p1, const void *p2)
{
int rc = do_sort_up_value_type(p1, p2);
/* use a tie-breaker if do_sort_up_value_type has no opinion */
return rc ? rc : sort_up_index_string(p1, p2);
}
/* sort_down_value_type --- qsort comparison function; descending value type */ /* sort_down_value_type --- qsort comparison function; descending value type */
static int static int
sort_down_value_type(const void *p1, const void *p2) sort_down_value_type(const void *p1, const void *p2)
{ {
return -sort_up_value_type(p1, p2); return -sort_up_value_type(p1, p2);
} }
/* sort_user_func --- user defined qsort comparison function */ /* sort_user_func --- user defined qsort comparison function */
 End of changes. 19 change blocks. 
38 lines changed or deleted 81 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)