From: Ben Pfaff Date: Sun, 13 Feb 2011 00:37:10 +0000 (-0800) Subject: i18n: New function recode_string_len(). X-Git-Tag: v0.7.7~24 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5099c58d17e8f66a74a84918e688ef17936d392;p=pspp-builds.git i18n: New function recode_string_len(). --- diff --git a/src/libpspp/i18n.c b/src/libpspp/i18n.c index 13a46a53..29148a0e 100644 --- a/src/libpspp/i18n.c +++ b/src/libpspp/i18n.c @@ -110,6 +110,17 @@ recode_string (const char *to, const char *from, return recode_string_pool (to, from, text, length, NULL); } +/* Returns the length, in bytes, of the string that a similar recode_string() + call would return. */ +size_t +recode_string_len (const char *to, const char *from, + const char *text, int length) +{ + char *s = recode_string (to, from, text, length); + size_t len = strlen (s); + free (s); + return len; +} /* Uses CONV to convert the INBYTES starting at IP into the OUTBYTES starting at OP, and appends a null terminator to the output. diff --git a/src/libpspp/i18n.h b/src/libpspp/i18n.h index 2f55a690..78a97378 100644 --- a/src/libpspp/i18n.h +++ b/src/libpspp/i18n.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2006, 2010 Free Software Foundation, Inc. + Copyright (C) 2006, 2010, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -41,6 +41,9 @@ char *recode_string_pool (const char *to, const char *from, struct substring recode_substring_pool (const char *to, const char *from, struct substring text, struct pool *); +size_t recode_string_len (const char *to, const char *from, + const char *text, int len); + bool valid_encoding (const char *enc); char get_system_decimal (void); diff --git a/tests/libpspp/i18n-test.c b/tests/libpspp/i18n-test.c index f2bead96..03b96f2e 100644 --- a/tests/libpspp/i18n-test.c +++ b/tests/libpspp/i18n-test.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2010 Free Software Foundation, Inc. + Copyright (C) 2010, 2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,9 +18,13 @@ #include #include +#include #include "libpspp/i18n.h" +#undef NDEBUG +#include + int main (int argc, char *argv[]) { @@ -40,6 +44,7 @@ main (int argc, char *argv[]) i18n_init (); s = recode_string (argv[2], argv[1], argv[3], -1); puts (s); + assert (strlen (s) == recode_string_len (argv[2], argv[1], argv[3], -1)); free (s); return 0;