Skip to content

Commit

Permalink
Fix whitespace:
Browse files Browse the repository at this point in the history
- no trailing space
- spaces, not tabs
  • Loading branch information
sourcefrog committed Jun 25, 2022
1 parent 10f2f84 commit f8a6b0c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 136 deletions.
142 changes: 71 additions & 71 deletions natsort.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* -*- mode: c; c-file-style: "k&r" -*-
natsort.c -- Example strnatcmp application.
Copyright (C) 2000 by Martin Pool <[email protected]>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
Expand Down Expand Up @@ -46,14 +46,14 @@ static void trace_result(char const *a, char const *b, int ret)
char const *op;

if (ret < 0)
op = "<";
op = "<";
else if (ret > 0)
op = ">";
op = ">";
else
op = "==";
op = "==";

fprintf(stderr, "\tstrncatcmp: \"%s\" %s \"%s\"\n",
a, op, b);
a, op, b);
}


Expand All @@ -64,15 +64,15 @@ static int compare_strings(const void *a, const void *b)
int ret;

if (fold_case)
ret = strnatcasecmp(pa, pb);
ret = strnatcasecmp(pa, pb);
else
ret = strnatcmp(pa, pb);
ret = strnatcmp(pa, pb);

if (reverse)
ret *= -1;
if (reverse)
ret *= -1;

if (verbose)
trace_result(pa, pb, ret);
if (verbose)
trace_result(pa, pb, ret);

return ret;
}
Expand All @@ -81,13 +81,13 @@ static int compare_strings(const void *a, const void *b)
static void usage(void)
{
fprintf(stderr, "Usage: natsort [OPTIONS]\n"
"Performs a natural sort on standard input, and writes to \n"
"standard output.\n"
"\n"
" --help, -h show help text\n"
" --verbose, -v show comparisons\n"
" --fold-case, -f ignore case differences for letters\n"
" --reverse, -r reverse the result of comparisons\n");
"Performs a natural sort on standard input, and writes to \n"
"standard output.\n"
"\n"
" --help, -h show help text\n"
" --verbose, -v show comparisons\n"
" --fold-case, -f ignore case differences for letters\n"
" --reverse, -r reverse the result of comparisons\n");
}


Expand All @@ -101,72 +101,72 @@ int main(int argc, char **argv)
size_t bufsize;

static struct option long_options[] = {
{ "verbose", 0, NULL, 'v'},
{ "reverse", 0, NULL, 'r'},
{ "fold-case", 0, NULL, 'f'},
{ "help", 0, 0, 'h' },
{ 0, 0, 0, 0 }
{ "verbose", 0, NULL, 'v'},
{ "reverse", 0, NULL, 'r'},
{ "fold-case", 0, NULL, 'f'},
{ "help", 0, 0, 'h' },
{ 0, 0, 0, 0 }
};

/* process arguments */
while ((c = getopt_long(argc, argv, "frvh", long_options, &opt_ind)) != -1) {
switch (c) {
case 'f':
fold_case = 1;
break;
case 'h':
usage();
return 0;
case 'r':
reverse = 1;
break;
case 'v':
verbose = 1;
break;
case '?':
return 1;
default:
abort();
}
}
while ((c = getopt_long(argc, argv, "frvh", long_options, &opt_ind)) != -1) {
switch (c) {
case 'f':
fold_case = 1;
break;
case 'h':
usage();
return 0;
case 'r':
reverse = 1;
break;
case 'v':
verbose = 1;
break;
case '?':
return 1;
default:
abort();
}
}

/* read lines into an array */
while (1) {
line = NULL;
bufsize = 0;
if ((linelen = getline(&line, &bufsize, stdin)) <= 0)
break;
if (line[linelen-1] == '\n')
line[--linelen] = 0;
nlines++;
rlist = (char **) realloc(list, nlines * sizeof list[0]);
if (!rlist) {
perror("allocate list");
free(list);
return 1;
}
list = rlist;
list[nlines-1] = line;
line = NULL;
bufsize = 0;
if ((linelen = getline(&line, &bufsize, stdin)) <= 0)
break;
if (line[linelen-1] == '\n')
line[--linelen] = 0;
nlines++;
rlist = (char **) realloc(list, nlines * sizeof list[0]);
if (!rlist) {
perror("allocate list");
free(list);
return 1;
}
list = rlist;
list[nlines-1] = line;
}

if (ferror(stdin)) {
perror("input");
free(list);
return 1;
perror("input");
free(list);
return 1;
}
fclose(stdin);

/* quicksort */
if (list)
qsort(list, nlines, sizeof list[0], compare_strings);
qsort(list, nlines, sizeof list[0], compare_strings);

/* and output */
for (i = 0; i < nlines; i++) {
puts(list[i]);
puts(list[i]);
}
if (ferror(stdout)) {
perror("output");
return 1;
perror("output");
return 1;
}

return 0;
Expand Down
128 changes: 64 additions & 64 deletions strnatcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* negative chars in their default char type.
*/

#include <stddef.h> /* size_t */
#include <stddef.h> /* size_t */
#include <ctype.h>

#include "strnatcmp.h"
Expand Down Expand Up @@ -63,26 +63,26 @@ static int
compare_right(nat_char const *a, nat_char const *b)
{
int bias = 0;

/* The longest run of digits wins. That aside, the greatest
value wins, but we can't know that it will until we've scanned
both numbers to know that they have the same magnitude, so we
remember it in BIAS. */
value wins, but we can't know that it will until we've scanned
both numbers to know that they have the same magnitude, so we
remember it in BIAS. */
for (;; a++, b++) {
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return bias;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b) {
if (!bias)
bias = -1;
} else if (*a > *b) {
if (!bias)
bias = +1;
} else if (!*a && !*b)
return bias;
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return bias;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b) {
if (!bias)
bias = -1;
} else if (*a > *b) {
if (!bias)
bias = +1;
} else if (!*a && !*b)
return bias;
}

return 0;
Expand All @@ -95,16 +95,16 @@ compare_left(nat_char const *a, nat_char const *b)
/* Compare two left-aligned numbers: the first to have a
different value wins. */
for (;; a++, b++) {
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return 0;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b)
return -1;
if (*a > *b)
return +1;
if (!nat_isdigit(*a) && !nat_isdigit(*b))
return 0;
if (!nat_isdigit(*a))
return -1;
if (!nat_isdigit(*b))
return +1;
if (*a < *b)
return -1;
if (*a > *b)
return +1;
}

return 0;
Expand All @@ -120,46 +120,46 @@ strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)

ai = bi = 0;
while (1) {
ca = a[ai]; cb = b[bi];

/* skip over leading spaces or zeros */
while (nat_isspace(ca))
ca = a[++ai];

while (nat_isspace(cb))
cb = b[++bi];

/* process run of digits */
if (nat_isdigit(ca) && nat_isdigit(cb)) {
fractional = (ca == '0' || cb == '0');

if (fractional) {
if ((result = compare_left(a+ai, b+bi)) != 0)
return result;
} else {
if ((result = compare_right(a+ai, b+bi)) != 0)
return result;
}
}

if (!ca && !cb) {
/* The strings compare the same. Perhaps the caller
ca = a[ai]; cb = b[bi];

/* skip over leading spaces or zeros */
while (nat_isspace(ca))
ca = a[++ai];

while (nat_isspace(cb))
cb = b[++bi];

/* process run of digits */
if (nat_isdigit(ca) && nat_isdigit(cb)) {
fractional = (ca == '0' || cb == '0');

if (fractional) {
if ((result = compare_left(a+ai, b+bi)) != 0)
return result;
} else {
if ((result = compare_right(a+ai, b+bi)) != 0)
return result;
}
}

if (!ca && !cb) {
/* The strings compare the same. Perhaps the caller
will want to call strcmp to break the tie. */
return 0;
}
return 0;
}

if (fold_case) {
ca = nat_toupper(ca);
cb = nat_toupper(cb);
}
if (fold_case) {
ca = nat_toupper(ca);
cb = nat_toupper(cb);
}

if (ca < cb)
return -1;
if (ca < cb)
return -1;

if (ca > cb)
return +1;
if (ca > cb)
return +1;

++ai; ++bi;
++ai; ++bi;
}
}

Expand Down
2 changes: 1 addition & 1 deletion strnatcmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef char nat_char;

int strnatcmp(nat_char const *a, nat_char const *b);
int strnatcasecmp(nat_char const *a, nat_char const *b);

#ifdef __cplusplus
}
#endif

0 comments on commit f8a6b0c

Please sign in to comment.