aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2011-04-24 12:45:47 +0300
committerArnold D. Robbins <arnold@skeeve.com>2011-04-24 12:45:47 +0300
commit8eb45b02e704c95866970005fe771e3507fb935c (patch)
treeffe48f1a5f46e15e6df6b7c509a592f7f9300f3f
parent203dfc888be3f45d3f70c9cdb91e870ae7796257 (diff)
downloadegawk-8eb45b02e704c95866970005fe771e3507fb935c.tar.gz
egawk-8eb45b02e704c95866970005fe771e3507fb935c.tar.bz2
egawk-8eb45b02e704c95866970005fe771e3507fb935c.zip
Patch from John, update man page.
-rw-r--r--ChangeLog7
-rw-r--r--array.c18
-rw-r--r--doc/gawk.122
3 files changed, 41 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 5fcbb96e..ed69432b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sun Apr 24 12:43:49 2011 John Haque <j.eh@mchsi.com>
+
+ * array.c (sort_user_func): Fix return value to match the
+ documentaion.
+ (sort_selection): Make user-specified comparison function with
+ the same name override default "unsorted" specification.
+
Fri Apr 22 16:05:27 2011 John Haque <j.eh@mchsi.com>
* array.c (sort_user_func): New routine to handle user-defined
diff --git a/array.c b/array.c
index 32b85065..1bf6a068 100644
--- a/array.c
+++ b/array.c
@@ -1371,8 +1371,8 @@ static int
sort_user_func(const void *p1, const void *p2)
{
const NODE *t1, *t2;
- NODE *idx1, *idx2, *val1, *val2, *r;
- int ret;
+ NODE *idx1, *idx2, *val1, *val2;
+ AWKNUM ret;
INSTRUCTION *code;
extern int exiting;
@@ -1403,11 +1403,9 @@ sort_user_func(const void *p1, const void *p2)
gawk_exit(exit_val);
/* return value of the comparison function */
- r = POP_SCALAR();
- ret = (int) force_number(r);
- DEREF(r);
+ POP_NUMBER(ret);
- return ret;
+ return (ret < 0.0) ? -1 : (ret > 0.0);
}
@@ -1522,6 +1520,14 @@ sort_selection(NODE *sort_str, SORT_CTXT sort_ctxt)
allparts |= bval;
}
+ if (allparts == Unsorted) {
+ NODE *f;
+ /* user-defined function overrides default */
+
+ if ((f = lookup(sort_str->stptr)) != NULL && f->type == Node_func)
+ return -1;
+ }
+
/* num_words <= 3 */
return (allparts & INDEX_MASK);
diff --git a/doc/gawk.1 b/doc/gawk.1
index 629899b1..3fdf5605 100644
--- a/doc/gawk.1
+++ b/doc/gawk.1
@@ -1098,6 +1098,25 @@ Supported values are
\fB"unsorted"\fR.
The order specification words can be truncated, or omitted (provided
that at least one is present), or given in any order.
+The value can also be the name of any comparison function defined
+as follows:
+.PP
+.RS
+\fBfunction cmp_func(i1, v1, i2, v2)\fR
+.RE
+.PP
+where
+.I i1
+and
+.I i2
+are the indices, and
+.I v1
+and
+.I v2
+are the
+corresponding values of the two elements being compared.
+It should return a number less than, equal to, or greater than 0,
+depending on how the elements of the array are to be ordered.
.TP
\fBPROCINFO["version"]\fP
the version of
@@ -2475,6 +2494,9 @@ Valid values for
.I how
are "ascending string", "ascending number",
"descending string" and "descending number".
+It can also be the name of a user-defined
+comparison function as described in
+\fBPROCINFO["sorted_in"]\fR.
.TP "\w'\fBsprintf(\^\fIfmt\fB\^, \fIexpr-list\^\fB)\fR'u+1n"
\fBasorti(\fIs \fR[\fB, \fId\fR [\fB, \fIhow\fR] ]\fB)\fR
Return the number of elements in the source