Changeset View
Standalone View
lib/libc/string/strverscmp.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause | |||||
* | |||||
* Copyright (c) 2022 Obiwac, | |||||
imp: Is this a company or a person? In any event, it should end with a '.' | |||||
obiwacAuthorUnsubmitted Done Inline ActionsJust my username. Is it a problem like this? obiwac: Just my username. Is it a problem like this? | |||||
impUnsubmitted Not Done Inline ActionsThe project has a strong preference for this to be either an actual person, or a company. imp: The project has a strong preference for this to be either an actual person, or a company.
If… | |||||
* All rights reserved. | |||||
impUnsubmitted Not Done Inline ActionsYou don't need this. imp: You don't need this. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
impUnsubmitted Not Done Inline ActionsSince this is boilerplate, I'd consider just using the SPDX identifier with a copyright. imp: Since this is boilerplate, I'd consider just using the SPDX identifier with a copyright.
| |||||
*/ | |||||
#if defined(LIBC_SCCS) && !defined(lint) | |||||
static char sccsid[] = "@(#)strverscmp.c 1.0 6/30/22"; | |||||
#endif /* LIBC_SCCS and not lint */ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
impUnsubmitted Not Done Inline ActionsDelete all of this. It's not needed. strverscmp.c never was in the CSRG SCCS tree and we don't put $FreeBSD$ in new code. imp: Delete all of this. It's not needed. strverscmp.c never was in the CSRG SCCS tree and we don't… | |||||
#include <ctype.h> | |||||
int strverscmp(const char* s1, const char* s2) { | |||||
pstefUnsubmitted Not Done Inline ActionsPlease see man 9 style how asterisks are supposed to be placed. pstef: Please see `man 9 style` how asterisks are supposed to be placed. | |||||
// if pointers are aliased, no need to go through to process of comparing them | |||||
pstefUnsubmitted Not Done Inline ActionsI don't think we allow BCPL-style comments yet. pstef: I don't think we allow BCPL-style comments yet. | |||||
impUnsubmitted Not Done Inline ActionsThe preferred style is /* .. */ still. imp: The preferred style is /* .. */ still.
| |||||
if (s1 == s2) { | |||||
return 0; | |||||
} | |||||
const unsigned char* u1 = (const void*) s1; | |||||
const unsigned char* u2 = (const void*) s2; | |||||
for (; *u1 && *u2; u1++, u2++) { | |||||
// leading zeroes; we're dealing with the fractional part of a number | |||||
if (*u1 == '0' || *u2 == '0') { | |||||
// count leading zeros (more leading zeroes == smaller number) | |||||
unsigned n1 = 0; | |||||
unsigned n2 = 0; | |||||
for (; *u1 == '0'; u1++) n1++; | |||||
for (; *u2 == '0'; u2++) n2++; | |||||
if (n1 != n2) { | |||||
return n2 - n1; | |||||
} | |||||
// handle the case where 000 < 09 | |||||
if (!*u1 && isdigit(*u2)) return 1; | |||||
if (!*u2 && isdigit(*u1)) return -1; | |||||
// for all other cases, compare each digit until there are none left | |||||
for (; isdigit(*u1) && isdigit(*u2); u1++, u2++) { | |||||
if (*u1 != *u2) { | |||||
return *u1 - *u2; | |||||
} | |||||
} | |||||
u1--, u2--; | |||||
} | |||||
// no leading; we're simply comparing two numbers | |||||
else if (isdigit(*u1) && isdigit(*u2)) { | |||||
impUnsubmitted Not Done Inline ActionsWe put the } and else on the same line. imp: We put the } and else on the same line.
I'd also move the comment to inside the if (and as with… | |||||
const unsigned char* o1 = u1; | |||||
const unsigned char* o2 = u2; | |||||
// count digits (more digits == larger number) | |||||
unsigned n1 = 0; | |||||
unsigned n2 = 0; | |||||
for (; isdigit(*u1); u1++) n1++; | |||||
impUnsubmitted Not Done Inline ActionsThese should have the n1++ and n2++ on a separate line. imp: These should have the n1++ and n2++ on a separate line. | |||||
for (; isdigit(*u2); u2++) n2++; | |||||
if (n1 != n2) { | |||||
return n1 - n2; | |||||
} | |||||
// if there're the same number of digits, | |||||
impUnsubmitted Not Done Inline ActionsMake this a block comment. imp: Make this a block comment. | |||||
// go back and compare each digit until there are none left | |||||
u1 = o1, u2 = o2; | |||||
impUnsubmitted Not Done Inline Actionscomma operator is confusing and buys nothing, imho. Just do two assignments. imp: comma operator is confusing and buys nothing, imho. Just do two assignments.
| |||||
for (; isdigit(*u1) && isdigit(*u2); u1++, u2++) { | |||||
if (*u1 != *u2) { | |||||
return *u1 - *u2; | |||||
} | |||||
} | |||||
u1--, u2--; | |||||
impUnsubmitted Not Done Inline Actionsditto. imp: ditto. | |||||
} | |||||
// for the rest, we can just fallback to a regular strcmp | |||||
if (*u1 != *u2) { | |||||
return *u1 - *u2; | |||||
} | |||||
} | |||||
return *u1 - *u2; | |||||
} |
Is this a company or a person? In any event, it should end with a '.'