Skip to content

Commit 79785e8

Browse files
authored
Merge pull request #1598 from Explorer09/init-degree-sign
CRT_degreeSign code "shrink"
2 parents e896ab4 + a52b43b commit 79785e8

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

CRT.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212
#include <errno.h>
1313
#include <fcntl.h>
1414
#include <langinfo.h>
15+
#include <limits.h>
1516
#include <signal.h>
1617
#include <stdarg.h>
1718
#include <stdio.h>
@@ -94,21 +95,31 @@ const char* const* CRT_treeStr = CRT_treeStrAscii;
9495

9596
static const Settings* CRT_settings;
9697

97-
const char* CRT_degreeSign;
98+
#ifdef HAVE_LIBNCURSESW
99+
# if MB_LEN_MAX >= 3 // Minimum required to support UTF-8 BMP subset
100+
char CRT_degreeSign[MB_LEN_MAX * 2] = "\xc2\xb0";
101+
# else
102+
char CRT_degreeSign[MB_LEN_MAX * 2] = "";
103+
# endif
104+
#else
105+
char CRT_degreeSign[] = "";
106+
#endif
98107

99-
static const char* initDegreeSign(void) {
108+
static void initDegreeSign(void) {
100109
#ifdef HAVE_LIBNCURSESW
110+
# if MB_LEN_MAX >= 3
101111
if (CRT_utf8)
102-
return "\xc2\xb0";
112+
return;
113+
# endif
103114

104-
static char buffer[4];
105115
// this might fail if the current locale does not support wide characters
106-
int r = snprintf(buffer, sizeof(buffer), "%lc", 176);
107-
if (r > 0)
108-
return buffer;
116+
int r = snprintf(CRT_degreeSign, sizeof(CRT_degreeSign), "%lc", 176);
117+
if (r <= 0 || (size_t)r >= sizeof(CRT_degreeSign))
118+
CRT_degreeSign[0] = '\0';
109119
#endif
110120

111-
return "";
121+
// No-op
122+
return;
112123
}
113124

114125
const int* CRT_colors;
@@ -1265,7 +1276,7 @@ IGNORE_WCASTQUAL_END
12651276

12661277
CRT_setMouse(settings->enableMouse);
12671278

1268-
CRT_degreeSign = initDegreeSign();
1279+
initDegreeSign();
12691280
}
12701281

12711282
void CRT_done(void) {

CRT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void CRT_handleSIGSEGV(int signal) ATTR_NORETURN;
182182
#define KEY_FOCUS_OUT (KEY_MAX + 'O')
183183
#define KEY_DEL_MAC 127
184184

185-
extern const char* CRT_degreeSign;
185+
extern char CRT_degreeSign[];
186186

187187
#ifdef HAVE_LIBNCURSESW
188188

0 commit comments

Comments
 (0)