xref: /freebsd/lib/libc/locale/fix_grouping.c (revision cb03ae3061835a849f9466a4047aa9787f8e99f0)
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>
311bd7723dSAlexey Zelkin 
321bd7723dSAlexey Zelkin static const char nogrouping[] = { CHAR_MAX, '\0' };
331bd7723dSAlexey Zelkin 
341bd7723dSAlexey Zelkin /*
351bd7723dSAlexey Zelkin  * "3;3;-1" -> "\003\003\177"
361bd7723dSAlexey Zelkin  */
371bd7723dSAlexey Zelkin 
381bd7723dSAlexey Zelkin const char *
391bd7723dSAlexey Zelkin __fix_locale_grouping_str(const char *str) {
401bd7723dSAlexey Zelkin 
411bd7723dSAlexey Zelkin 	char *src, *dst;
42cb03ae30SAlexey Zelkin 	char n;
431bd7723dSAlexey Zelkin 
441bd7723dSAlexey Zelkin 	if (str == 0) {
451bd7723dSAlexey Zelkin 		return nogrouping;
461bd7723dSAlexey Zelkin 	}
471bd7723dSAlexey Zelkin 	for (src = (char*)str, dst = (char*)str; *src; src++) {
481bd7723dSAlexey Zelkin 		char cur;
491bd7723dSAlexey Zelkin 
501bd7723dSAlexey Zelkin 		/* input string examples: "3;3", "3;2;-1" */
511bd7723dSAlexey Zelkin 		if (*src == ';')
521bd7723dSAlexey Zelkin 			continue;
531bd7723dSAlexey Zelkin 
541bd7723dSAlexey Zelkin 		if (*src == '-' && *(src+1) == '1') {
551bd7723dSAlexey Zelkin 			*dst++ = CHAR_MAX;
561bd7723dSAlexey Zelkin 			src++;
571bd7723dSAlexey Zelkin 			continue;
581bd7723dSAlexey Zelkin 		}
591bd7723dSAlexey Zelkin 
60cb03ae30SAlexey Zelkin 		if (!isdigit((unsigned char)*src)) {
611bd7723dSAlexey Zelkin 			/* broken grouping string */
621bd7723dSAlexey Zelkin 			return nogrouping;
631bd7723dSAlexey Zelkin 		}
641bd7723dSAlexey Zelkin 
65cb03ae30SAlexey Zelkin 		for (n = 0; isdigit((unsigned char)*src); src++) {
66cb03ae30SAlexey Zelkin 			n *= 10;
67cb03ae30SAlexey Zelkin 			n += *src - '0';
68cb03ae30SAlexey Zelkin 		}
69cb03ae30SAlexey Zelkin 
70cb03ae30SAlexey Zelkin 		*dst++ = n;
711bd7723dSAlexey Zelkin 	}
721bd7723dSAlexey Zelkin 	*dst = '\0';
731bd7723dSAlexey Zelkin 	return str;
741bd7723dSAlexey Zelkin }
75