xref: /freebsd/lib/libc/locale/fix_grouping.c (revision b21656a8f42cc73059233203a718f03b275bdd9c)
11bd7723dSAlexey Zelkin /*
21bd7723dSAlexey Zelkin  * Copyright (c) 2001 Alexey Zelkin
31bd7723dSAlexey Zelkin  * All rights reserved.
41bd7723dSAlexey Zelkin  *
51bd7723dSAlexey Zelkin  * Redistribution and use in source and binary forms, with or without
61bd7723dSAlexey Zelkin  * modification, are permitted provided that the following conditions
71bd7723dSAlexey Zelkin  * are met:
81bd7723dSAlexey Zelkin  * 1. Redistributions of source code must retain the above copyright
91bd7723dSAlexey Zelkin  *    notice, this list of conditions and the following disclaimer.
101bd7723dSAlexey Zelkin  * 2. Redistributions in binary form must reproduce the above copyright
111bd7723dSAlexey Zelkin  *    notice, this list of conditions and the following disclaimer in the
121bd7723dSAlexey Zelkin  *    documentation and/or other materials provided with the distribution.
131bd7723dSAlexey Zelkin  *
141bd7723dSAlexey Zelkin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151bd7723dSAlexey Zelkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161bd7723dSAlexey Zelkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171bd7723dSAlexey Zelkin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181bd7723dSAlexey Zelkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191bd7723dSAlexey Zelkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201bd7723dSAlexey Zelkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211bd7723dSAlexey Zelkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221bd7723dSAlexey Zelkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231bd7723dSAlexey Zelkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241bd7723dSAlexey Zelkin  * SUCH DAMAGE.
251bd7723dSAlexey Zelkin  *
261bd7723dSAlexey Zelkin  * $FreeBSD$
271bd7723dSAlexey Zelkin  */
281bd7723dSAlexey Zelkin 
29cb03ae30SAlexey Zelkin #include <ctype.h>
301bd7723dSAlexey Zelkin #include <limits.h>
31ba4a5c92SAlexey Zelkin #include <stddef.h>
321bd7723dSAlexey Zelkin 
331bd7723dSAlexey Zelkin static const char nogrouping[] = { CHAR_MAX, '\0' };
341bd7723dSAlexey Zelkin 
351bd7723dSAlexey Zelkin /*
361bd7723dSAlexey Zelkin  * "3;3;-1" -> "\003\003\177"
371bd7723dSAlexey Zelkin  */
381bd7723dSAlexey Zelkin 
391bd7723dSAlexey Zelkin const char *
401bd7723dSAlexey Zelkin __fix_locale_grouping_str(const char *str) {
411bd7723dSAlexey Zelkin 
421bd7723dSAlexey Zelkin 	char *src, *dst;
43cb03ae30SAlexey Zelkin 	char n;
441bd7723dSAlexey Zelkin 
45ba4a5c92SAlexey Zelkin 	if (str == NULL || *str == '\0') {
461bd7723dSAlexey Zelkin 		return nogrouping;
471bd7723dSAlexey Zelkin 	}
48ba4a5c92SAlexey Zelkin 
49ba4a5c92SAlexey Zelkin 	for (src = (char*)str, dst = (char*)str; *src != '\0'; src++) {
501bd7723dSAlexey Zelkin 
511bd7723dSAlexey Zelkin 		/* input string examples: "3;3", "3;2;-1" */
521bd7723dSAlexey Zelkin 		if (*src == ';')
531bd7723dSAlexey Zelkin 			continue;
541bd7723dSAlexey Zelkin 
551bd7723dSAlexey Zelkin 		if (*src == '-' && *(src+1) == '1') {
561bd7723dSAlexey Zelkin 			*dst++ = CHAR_MAX;
571bd7723dSAlexey Zelkin 			src++;
581bd7723dSAlexey Zelkin 			continue;
591bd7723dSAlexey Zelkin 		}
601bd7723dSAlexey Zelkin 
61cb03ae30SAlexey Zelkin 		if (!isdigit((unsigned char)*src)) {
621bd7723dSAlexey Zelkin 			/* broken grouping string */
631bd7723dSAlexey Zelkin 			return nogrouping;
641bd7723dSAlexey Zelkin 		}
651bd7723dSAlexey Zelkin 
66b21656a8SAlexey Zelkin 		/* assume all numbers <= 99 */
67b21656a8SAlexey Zelkin 		n = *src - '0';
68b21656a8SAlexey Zelkin 		if (isdigit((unsigned char)*(src+1))) {
69b21656a8SAlexey Zelkin 			src++;
70cb03ae30SAlexey Zelkin 			n *= 10;
71cb03ae30SAlexey Zelkin 			n += *src - '0';
72cb03ae30SAlexey Zelkin 		}
73cb03ae30SAlexey Zelkin 
74c80a9160SAndrey A. Chernov 		*dst = n;
75c80a9160SAndrey A. Chernov 		/* NOTE: assume all input started with "0" as 'no grouping' */
76c80a9160SAndrey A. Chernov 		if (*dst == '\0')
77c80a9160SAndrey A. Chernov 			return (dst == (char*)str) ? nogrouping : str;
78c80a9160SAndrey A. Chernov 		dst++;
791bd7723dSAlexey Zelkin 	}
801bd7723dSAlexey Zelkin 	*dst = '\0';
811bd7723dSAlexey Zelkin 	return str;
821bd7723dSAlexey Zelkin }
83