make: add :Or for reverse sort

:Or is more efficient than :O:[-1..1]

Reviewed by: christos
This commit is contained in:
sjg 2020-06-05 19:20:46 +00:00
parent 62b57d3e75
commit 39bf2cb2a2
2 changed files with 23 additions and 10 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: make.1,v 1.280 2020/04/27 20:03:08 christos Exp $
.\" $NetBSD: make.1,v 1.281 2020/06/05 19:20:46 sjg Exp $
.\"
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" from: @(#)make.1 8.4 (Berkeley) 3/19/94
.\"
.Dd April 27, 2020
.Dd June 5, 2020
.Dt MAKE 1
.Os
.Sh NAME
@ -1195,10 +1195,8 @@ but selects all words which do not match
.Ar pattern .
.It Cm \&:O
Order every word in variable alphabetically.
To sort words in
reverse order use the
.Ql Cm \&:O:[-1..1]
combination of modifiers.
.It Cm \&:Or
Order every word in variable in reverse alphabetical order.
.It Cm \&:Ox
Randomize words in variable.
The results will be different each time you are referring to the
@ -1613,6 +1611,11 @@ then the words are output in reverse order.
For example,
.Ql Cm \&:[-1..1]
selects all the words from last to first.
If the list is already ordered, then this effectively reverses
the list, but it is more efficient to use
.Ql Cm \&:Or
instead of
.Ql Cm \&:O:[-1..1] .
.\" :[*]
.It Cm \&*
Causes subsequent modifiers to treat the value as a single word

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.223 2020/04/25 18:20:57 christos Exp $ */
/* $NetBSD: var.c,v 1.224 2020/06/05 19:20:46 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: var.c,v 1.223 2020/04/25 18:20:57 christos Exp $";
static char rcsid[] = "$NetBSD: var.c,v 1.224 2020/06/05 19:20:46 sjg Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: var.c,v 1.223 2020/04/25 18:20:57 christos Exp $");
__RCSID("$NetBSD: var.c,v 1.224 2020/06/05 19:20:46 sjg Exp $");
#endif
#endif /* not lint */
#endif
@ -2028,6 +2028,13 @@ VarWordCompare(const void *a, const void *b)
return r;
}
static int
VarWordCompareReverse(const void *a, const void *b)
{
int r = strcmp(*(const char * const *)b, *(const char * const *)a);
return r;
}
/*-
*-----------------------------------------------------------------------
* VarOrder --
@ -2059,6 +2066,9 @@ VarOrder(const char *str, const char otype)
if (ac > 0)
switch (otype) {
case 'r': /* reverse sort alphabetically */
qsort(av, ac, sizeof(char *), VarWordCompareReverse);
break;
case 's': /* sort alphabetically */
qsort(av, ac, sizeof(char *), VarWordCompare);
break;
@ -3563,7 +3573,7 @@ ApplyModifiers(char *nstr, const char *tstr,
if (tstr[1] == endc || tstr[1] == ':') {
otype = 's';
termc = *cp;
} else if ( (tstr[1] == 'x') &&
} else if ( (tstr[1] == 'r' || tstr[1] == 'x') &&
(tstr[2] == endc || tstr[2] == ':') ) {
otype = tstr[1];
cp = tstr + 2;