aboutsummaryrefslogtreecommitdiffhomepage
path: root/common.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-03-03 15:20:30 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-03-03 15:22:03 -0800
commit3ead99b0883c49d465a90a6746409003d7224190 (patch)
treefad0ab467226584b975328b305fded21b980239e /common.cpp
parent8c0803e3c5d9cb874f5dd69ee2727eb4be99bfac (diff)
Put fish on a diet. Tracked down the biggest memory hogs and fixed them. Total allocations down by a factor of 3 or so, live allocations a few KB.
Diffstat (limited to 'common.cpp')
-rw-r--r--common.cpp38
1 files changed, 33 insertions, 5 deletions
diff --git a/common.cpp b/common.cpp
index 012bf13c..0bdb2035 100644
--- a/common.cpp
+++ b/common.cpp
@@ -726,18 +726,19 @@ void debug_safe(int level, const char *msg, const char *param1, const char *para
errno = errno_old;
}
-void format_int_safe(char buff[128], int val) {
+void format_long_safe(char buff[128], long val) {
if (val == 0) {
strcpy(buff, "0");
} else {
/* Generate the string in reverse */
size_t idx = 0;
bool negative = (val < 0);
- if (negative)
- val = -val;
+
+ /* Note that we can't just negate val if it's negative, because it may be the most negative value. We do rely on round-towards-zero division though. */
- while (val > 0) {
- buff[idx++] = '0' + (val % 10);
+ while (val != 0) {
+ long rem = val % 10;
+ buff[idx++] = '0' + (rem < 0 ? -rem : rem);
val /= 10;
}
if (negative)
@@ -753,6 +754,33 @@ void format_int_safe(char buff[128], int val) {
}
}
+void format_long_safe(wchar_t buff[128], long val) {
+ if (val == 0) {
+ wcscpy(buff, L"0");
+ } else {
+ /* Generate the string in reverse */
+ size_t idx = 0;
+ bool negative = (val < 0);
+
+ while (val > 0) {
+ long rem = val % 10;
+ /* Here we're assuming that wide character digits are contiguous - is that a correct assumption? */
+ buff[idx++] = L'0' + (rem < 0 ? -rem : rem);
+ val /= 10;
+ }
+ if (negative)
+ buff[idx++] = L'-';
+ buff[idx] = 0;
+
+ size_t left = 0, right = idx - 1;
+ while (left < right) {
+ wchar_t tmp = buff[left];
+ buff[left++] = buff[right];
+ buff[right--] = tmp;
+ }
+ }
+}
+
void write_screen( const wcstring &msg, wcstring &buff )
{
const wchar_t *start, *pos;