lib/strutil/strutilascii.c: cosmetics and minor refactoring.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-07-12 20:50:26 +04:00
parent 5b91108b3c
commit 9e4d279885

View File

@ -73,26 +73,22 @@ str_ascii_cprev_char (const char **text)
static int
str_ascii_cnext_noncomb_char (const char **text)
{
if (*text[0] != '\0')
{
if (*text[0] == '\0')
return 0;
(*text)++;
return 1;
}
else
return 0;
}
static int
str_ascii_cprev_noncomb_char (const char **text, const char *begin)
{
if ((*text) != begin)
{
if ((*text) == begin)
return 0;
(*text)--;
return 1;
}
else
return 0;
}
static int
str_ascii_isspace (const char *text)
@ -136,6 +132,7 @@ str_ascii_toupper (const char *text, char **out, size_t * remain)
{
if (*remain <= 1)
return 0;
(*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
(*out)++;
(*remain)--;
@ -147,6 +144,7 @@ str_ascii_tolower (const char *text, char **out, size_t * remain)
{
if (*remain <= 1)
return 0;
(*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
(*out)++;
(*remain)--;
@ -169,7 +167,7 @@ static gchar *
str_ascii_conv_gerror_message (GError * error, const char *def_msg)
{
/* the same as str_utf8_conv_gerror_message() */
if ((error != NULL) && (error->message != NULL))
if (error != NULL)
return g_strdup (error->message);
return g_strdup (def_msg != NULL ? def_msg : "");
@ -183,7 +181,6 @@ str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString *
return ESTR_SUCCESS;
}
static const char *
str_ascii_term_form (const char *text)
{
@ -214,7 +211,7 @@ str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
static char result[BUF_MEDIUM];
char *actual;
size_t remain;
int ident;
int ident = 0;
size_t length;
size_t pos = 0;
@ -224,7 +221,6 @@ str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
if ((int) length <= width)
{
ident = 0;
switch (HIDE_FIT (just_mode))
{
case J_CENTER_LEFT:
@ -259,9 +255,7 @@ str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
actual += width - length - ident;
}
}
else
{
if (IS_FIT (just_mode))
else if (IS_FIT (just_mode))
{
/* copy prefix of text, that is not wider than width / 2 */
for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
@ -287,7 +281,6 @@ str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
}
else
{
ident = 0;
switch (HIDE_FIT (just_mode))
{
case J_CENTER:
@ -308,7 +301,7 @@ str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
}
}
}
finally:
actual[0] = '\0';
return result;
@ -330,9 +323,16 @@ str_ascii_term_trim (const char *text, int width)
if (width > 0)
{
if (width < (int) length)
if (width >= (int) length)
{
if (width <= 3)
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
else if (width <= 3)
{
memset (actual, '.', width);
actual += width;
@ -353,16 +353,6 @@ str_ascii_term_trim (const char *text, int width)
}
}
}
else
{
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
}
actual[0] = '\0';
return result;
@ -414,9 +404,7 @@ str_ascii_term_substring (const char *text, int start, int width)
/* if text is shorter then width, add space to the end */
for (; width > 0 && remain > 1; actual++, remain--, width--)
{
actual[0] = ' ';
}
actual[0] = '\0';
return result;
@ -585,10 +573,8 @@ static void
str_ascii_fix_string (char *text)
{
for (; text[0] != '\0'; text++)
{
text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
}
}
static char *
str_ascii_create_key (const char *text, int case_sen)
@ -614,8 +600,10 @@ static int
str_ascii_prefix (const char *text, const char *prefix)
{
int result;
for (result = 0; text[result] != '\0' && prefix[result] != '\0'
&& text[result] == prefix[result]; result++);
return result;
}
@ -623,12 +611,13 @@ static int
str_ascii_caseprefix (const char *text, const char *prefix)
{
int result;
for (result = 0; text[result] != '\0' && prefix[result] != '\0'
&& g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);
return result;
}
struct str_class
str_ascii_init (void)
{