xref: /freebsd/lib/libc/locale/fix_grouping.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
1*d915a14eSPedro F. Giffuni /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro F. Giffuni  *
4f2d0f427SAlexey Zelkin  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
51bd7723dSAlexey Zelkin  * All rights reserved.
61bd7723dSAlexey Zelkin  *
71bd7723dSAlexey Zelkin  * Redistribution and use in source and binary forms, with or without
81bd7723dSAlexey Zelkin  * modification, are permitted provided that the following conditions
91bd7723dSAlexey Zelkin  * are met:
101bd7723dSAlexey Zelkin  * 1. Redistributions of source code must retain the above copyright
111bd7723dSAlexey Zelkin  *    notice, this list of conditions and the following disclaimer.
121bd7723dSAlexey Zelkin  * 2. Redistributions in binary form must reproduce the above copyright
131bd7723dSAlexey Zelkin  *    notice, this list of conditions and the following disclaimer in the
141bd7723dSAlexey Zelkin  *    documentation and/or other materials provided with the distribution.
151bd7723dSAlexey Zelkin  *
161bd7723dSAlexey Zelkin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171bd7723dSAlexey Zelkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181bd7723dSAlexey Zelkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191bd7723dSAlexey Zelkin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201bd7723dSAlexey Zelkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211bd7723dSAlexey Zelkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221bd7723dSAlexey Zelkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231bd7723dSAlexey Zelkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241bd7723dSAlexey Zelkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251bd7723dSAlexey Zelkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261bd7723dSAlexey Zelkin  * SUCH DAMAGE.
271bd7723dSAlexey Zelkin  */
281bd7723dSAlexey Zelkin 
29333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
30333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31333fc21eSDavid E. O'Brien 
32cb03ae30SAlexey Zelkin #include <ctype.h>
331bd7723dSAlexey Zelkin #include <limits.h>
34ba4a5c92SAlexey Zelkin #include <stddef.h>
351bd7723dSAlexey Zelkin 
361bd7723dSAlexey Zelkin static const char nogrouping[] = { CHAR_MAX, '\0' };
371bd7723dSAlexey Zelkin 
381bd7723dSAlexey Zelkin /*
39683fe113SAlexey Zelkin  * Internal helper used to convert grouping sequences from string
40683fe113SAlexey Zelkin  * representation into POSIX specified form, i.e.
41683fe113SAlexey Zelkin  *
42683fe113SAlexey Zelkin  * "3;3;-1" -> "\003\003\177\000"
431bd7723dSAlexey Zelkin  */
441bd7723dSAlexey Zelkin 
451bd7723dSAlexey Zelkin const char *
46683fe113SAlexey Zelkin __fix_locale_grouping_str(const char *str)
47683fe113SAlexey Zelkin {
481bd7723dSAlexey Zelkin 	char *src, *dst;
49cb03ae30SAlexey Zelkin 	char n;
501bd7723dSAlexey Zelkin 
51ba4a5c92SAlexey Zelkin 	if (str == NULL || *str == '\0') {
521bd7723dSAlexey Zelkin 		return nogrouping;
531bd7723dSAlexey Zelkin 	}
54ba4a5c92SAlexey Zelkin 
55ba4a5c92SAlexey Zelkin 	for (src = (char*)str, dst = (char*)str; *src != '\0'; src++) {
561bd7723dSAlexey Zelkin 
571bd7723dSAlexey Zelkin 		/* input string examples: "3;3", "3;2;-1" */
581bd7723dSAlexey Zelkin 		if (*src == ';')
591bd7723dSAlexey Zelkin 			continue;
601bd7723dSAlexey Zelkin 
611bd7723dSAlexey Zelkin 		if (*src == '-' && *(src+1) == '1') {
621bd7723dSAlexey Zelkin 			*dst++ = CHAR_MAX;
631bd7723dSAlexey Zelkin 			src++;
641bd7723dSAlexey Zelkin 			continue;
651bd7723dSAlexey Zelkin 		}
661bd7723dSAlexey Zelkin 
67cb03ae30SAlexey Zelkin 		if (!isdigit((unsigned char)*src)) {
681bd7723dSAlexey Zelkin 			/* broken grouping string */
691bd7723dSAlexey Zelkin 			return nogrouping;
701bd7723dSAlexey Zelkin 		}
711bd7723dSAlexey Zelkin 
72b21656a8SAlexey Zelkin 		/* assume all numbers <= 99 */
73b21656a8SAlexey Zelkin 		n = *src - '0';
74b21656a8SAlexey Zelkin 		if (isdigit((unsigned char)*(src+1))) {
75b21656a8SAlexey Zelkin 			src++;
76cb03ae30SAlexey Zelkin 			n *= 10;
77cb03ae30SAlexey Zelkin 			n += *src - '0';
78cb03ae30SAlexey Zelkin 		}
79cb03ae30SAlexey Zelkin 
80c80a9160SAndrey A. Chernov 		*dst = n;
81c80a9160SAndrey A. Chernov 		/* NOTE: assume all input started with "0" as 'no grouping' */
82c80a9160SAndrey A. Chernov 		if (*dst == '\0')
83c80a9160SAndrey A. Chernov 			return (dst == (char*)str) ? nogrouping : str;
84c80a9160SAndrey A. Chernov 		dst++;
851bd7723dSAlexey Zelkin 	}
861bd7723dSAlexey Zelkin 	*dst = '\0';
871bd7723dSAlexey Zelkin 	return str;
881bd7723dSAlexey Zelkin }
89