Skip to content

Commit

Permalink
Fix display non-ascii character (jonas#1299)
Browse files Browse the repository at this point in the history
[tk: added missing occurences and fixed behaviour on macOS]
  • Loading branch information
koutcher committed Oct 28, 2023
2 parents 13f463b + 8b4be8d commit 4ef6807
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
11 changes: 10 additions & 1 deletion include/tig/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@
#define TIG_STRING_H

#include "tig/tig.h"
#include "tig/string.h"

/*
* Strings.
*/

#if defined(__APPLE__)
/* See COMPATIBILITY in isspace(3). */
#define isalnum(c) iswalnum(btowc(c))
#define iscntrl(c) iswcntrl(btowc(c))
#define isdigit(c) iswdigit(btowc(c))
#define ispunct(c) iswpunct(btowc(c))
#define isspace(c) iswspace(btowc(c))
#define isxdigit(c) iswxdigit(btowc(c))
#endif

#define prefixcmp(str1, str2) \
strncmp(str1, str2, STRING_SIZE(str2))

Expand Down
4 changes: 4 additions & 0 deletions include/tig/tig.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
#include <libgen.h>
#include <termios.h>

#if defined(__APPLE__)
#include <wchar.h>
#endif

#if defined HAVE_PCRE2
#include <pcre2posix.h>
#elif defined HAVE_PCRE
Expand Down
4 changes: 2 additions & 2 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ parse_arg(char **cmd, bool remove_quotes)
break;
}

if (!quote && isspace(c))
if (!quote && isspace((unsigned char)c))
break;

*next++ = *pos;
Expand Down Expand Up @@ -315,7 +315,7 @@ format_expand_arg(struct format_context *format, const char *name, const char *e
if (end && msglen > 0 && string_format(msgbuf, "%.*s", msglen, msgstart)) {
const char *msg = msgbuf;

while (isspace(*msg))
while (isspace((unsigned char)*msg))
msg++;
if (*msg)
prompt = msg;
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ main_read(struct view *view, struct buffer *buf, bool force_stop)
state->in_header = true;
line += STRING_SIZE("commit ");
is_boundary = *line == '-';
while (*line && !isalnum(*line))
while (*line && !isalnum((unsigned char)*line))
line++;

main_flush_commit(view, commit);
Expand Down Expand Up @@ -524,7 +524,7 @@ main_read(struct view *view, struct buffer *buf, bool force_stop)
line += 4;
/* Well, if the title starts with a whitespace character,
* try to be forgiving. Otherwise we end up with no title. */
while (isspace(*line))
while (isspace((unsigned char)*line))
line++;
if (*line == '\0')
break;
Expand Down
2 changes: 1 addition & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ parse_step(double *opt, const char *arg)
{
int value = atoi(arg);

if (!value && !isdigit(*arg))
if (!value && !isdigit((unsigned char)*arg))
return error("Invalid double or percentage");

*opt = value;
Expand Down
2 changes: 1 addition & 1 deletion src/pager.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pager_common_read(struct view *view, const char *data, enum line_type type, stru

if (line->type == LINE_COMMIT && view_has_flags(view, VIEW_ADD_PAGER_REFS)) {
data += STRING_SIZE("commit ");
while (*data && !isalnum(*data))
while (*data && !isalnum((unsigned char)*data))
data++;
add_pager_refs(view, data);
}
Expand Down
6 changes: 3 additions & 3 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ parse_size(const char *text)
while (*text == ' ')
text++;

while (isdigit(*text))
while (isdigit((unsigned char)*text))
size = (size * 10) + (*text++ - '0');

return size;
Expand Down Expand Up @@ -98,7 +98,7 @@ parse_number(const char **posref, size_t *number)

*posref = NULL;
pos = strchr(pos + 1, ' ');
if (!pos || !isdigit(pos[1]))
if (!pos || !isdigit((unsigned char)pos[1]))
return false;
*number = atoi(pos + 1);

Expand Down Expand Up @@ -197,7 +197,7 @@ parse_ulong(const char **pos_ptr, unsigned long *value, char skip, bool optional
if (end == start)
return false;

while (isspace(*end))
while (isspace((unsigned char)*end))
end++;
*pos_ptr = end;
return true;
Expand Down
16 changes: 8 additions & 8 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ string_isnumber(const char *str)
int pos;

for (pos = 0; str[pos]; pos++) {
if (!isdigit(str[pos]))
if (!isdigit((unsigned char)str[pos]))
return false;
}

Expand All @@ -38,7 +38,7 @@ iscommit(const char *str)
int pos;

for (pos = 0; str[pos]; pos++) {
if (!isxdigit(str[pos]))
if (!isxdigit((unsigned char)str[pos]))
return false;
}

Expand Down Expand Up @@ -73,7 +73,7 @@ string_copy_rev(char *dst, const char *src)
return;

for (srclen = 0; srclen < SIZEOF_REV; srclen++)
if (!src[srclen] || isspace(src[srclen]))
if (!src[srclen] || isspace((unsigned char)src[srclen]))
break;

string_ncopy_do(dst, SIZEOF_REV, src, srclen);
Expand All @@ -83,7 +83,7 @@ void
string_copy_rev_from_commit_line(char *dst, const char *src)
{
src += STRING_SIZE("commit ");
while (*src && !isalnum(*src))
while (*src && !isalnum((unsigned char)*src))
src++;
string_copy_rev(dst, src);
}
Expand All @@ -103,7 +103,7 @@ string_expand(char *dst, size_t dstlen, const char *src, int srclen, int tabsize
expanded = dstlen - size - 1;
memcpy(dst + size, " ", expanded);
size += expanded;
} else if (isspace(c) || iscntrl(c)) {
} else if (isspace((unsigned char)c) || iscntrl((unsigned char)c)) {
dst[size++] = ' ';
} else {
dst[size++] = src[pos];
Expand All @@ -119,7 +119,7 @@ string_trim_end(char *name)
{
int namelen = strlen(name) - 1;

while (namelen > 0 && isspace(name[namelen]))
while (namelen > 0 && isspace((unsigned char)name[namelen]))
name[namelen--] = 0;

return name;
Expand All @@ -128,7 +128,7 @@ string_trim_end(char *name)
char *
string_trim(char *name)
{
while (isspace(*name))
while (isspace((unsigned char)*name))
name++;

return string_trim_end(name);
Expand Down Expand Up @@ -166,7 +166,7 @@ strcmp_numeric(const char *s1, const char *s2)
for (; *s1 && *s2 && *s1 == *s2; s1++, s2++) {
int c = *s1;

if (isdigit(c)) {
if (isdigit((unsigned char)c)) {
number = 10 * number + (c - '0');
} else {
number = 0;
Expand Down

0 comments on commit 4ef6807

Please sign in to comment.