forked from github/server
fix an out-of-bounds error in the new stream-based report code when centering very short headlines, refactor indentation code, add tests.
This commit is contained in:
parent
d0f8825240
commit
e7661434e3
20
src/report.c
20
src/report.c
|
@ -120,7 +120,14 @@ void newline(stream *out) {
|
||||||
sputs("", out);
|
sputs("", out);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *spaces = " ";
|
void write_spaces(stream *out, size_t num) {
|
||||||
|
static const char spaces[REPORTWIDTH] = " ";
|
||||||
|
while (num > 0) {
|
||||||
|
size_t bytes = (num > REPORTWIDTH) ? REPORTWIDTH : num;
|
||||||
|
swrite(spaces, sizeof(char), bytes, out);
|
||||||
|
num -= bytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void centre(stream *out, const char *s, bool breaking)
|
static void centre(stream *out, const char *s, bool breaking)
|
||||||
{
|
{
|
||||||
|
@ -140,9 +147,8 @@ static void centre(stream *out, const char *s, bool breaking)
|
||||||
freestrlist(T);
|
freestrlist(T);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
swrite(spaces, sizeof(char), (REPORTWIDTH - strlen(s) + 1) / 2, out);
|
write_spaces(out, (REPORTWIDTH - strlen(s) + 1) / 2);
|
||||||
sputs(s, out);
|
sputs(s, out);
|
||||||
newline(out);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,16 +182,16 @@ char marker)
|
||||||
const char *last_space = begin;
|
const char *last_space = begin;
|
||||||
|
|
||||||
if (mark && indent >= 2) {
|
if (mark && indent >= 2) {
|
||||||
swrite(spaces, sizeof(char), indent - 2, out);
|
write_spaces(out, indent - 2);
|
||||||
swrite(mark, sizeof(char), 1, out);
|
swrite(mark, sizeof(char), 1, out);
|
||||||
swrite(spaces, sizeof(char), 1, out);
|
write_spaces(out, 1);
|
||||||
mark = 0;
|
mark = 0;
|
||||||
}
|
}
|
||||||
else if (begin == str) {
|
else if (begin == str) {
|
||||||
swrite(spaces, sizeof(char), indent, out);
|
write_spaces(out, indent);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
swrite(spaces, sizeof(char), indent + hanging_indent, out);
|
write_spaces(out, indent + hanging_indent);
|
||||||
}
|
}
|
||||||
while (*end && end <= begin + length - indent) {
|
while (*end && end <= begin + length - indent) {
|
||||||
if (*end == ' ') {
|
if (*end == ' ') {
|
||||||
|
|
|
@ -15,8 +15,10 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct stream;
|
||||||
void register_nr(void);
|
void register_nr(void);
|
||||||
void report_cleanup(void);
|
void report_cleanup(void);
|
||||||
|
void write_spaces(struct stream *out, size_t num);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <platform.h>
|
#include <platform.h>
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include "reports.h"
|
#include "reports.h"
|
||||||
|
#include "report.h"
|
||||||
|
|
||||||
#include <kernel/building.h>
|
#include <kernel/building.h>
|
||||||
#include <kernel/faction.h>
|
#include <kernel/faction.h>
|
||||||
|
@ -10,6 +11,8 @@
|
||||||
#include <kernel/unit.h>
|
#include <kernel/unit.h>
|
||||||
|
|
||||||
#include <quicklist.h>
|
#include <quicklist.h>
|
||||||
|
#include <stream.h>
|
||||||
|
#include <memstream.h>
|
||||||
|
|
||||||
#include <CuTest.h>
|
#include <CuTest.h>
|
||||||
#include <tests.h>
|
#include <tests.h>
|
||||||
|
@ -98,11 +101,43 @@ static void test_seen_faction(CuTest *tc) {
|
||||||
CuAssertTrue(tc, f1->no < f2->no);
|
CuAssertTrue(tc, f1->no < f2->no);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_write_spaces(CuTest *tc) {
|
||||||
|
stream out = { 0 };
|
||||||
|
char buf[1024];
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
mstream_init(&out);
|
||||||
|
write_spaces(&out, 4);
|
||||||
|
out.api->rewind(out.handle);
|
||||||
|
len = out.api->read(out.handle, buf, sizeof(buf));
|
||||||
|
buf[len] = '\0';
|
||||||
|
CuAssertStrEquals(tc, " ", buf);
|
||||||
|
CuAssertIntEquals(tc, ' ', buf[3]);
|
||||||
|
mstream_done(&out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_write_many_spaces(CuTest *tc) {
|
||||||
|
stream out = { 0 };
|
||||||
|
char buf[1024];
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
mstream_init(&out);
|
||||||
|
write_spaces(&out, 100);
|
||||||
|
out.api->rewind(out.handle);
|
||||||
|
len = out.api->read(out.handle, buf, sizeof(buf));
|
||||||
|
buf[len] = '\0';
|
||||||
|
CuAssertIntEquals(tc, 100, (int)len);
|
||||||
|
CuAssertIntEquals(tc, ' ', buf[99]);
|
||||||
|
mstream_done(&out);
|
||||||
|
}
|
||||||
|
|
||||||
CuSuite *get_reports_suite(void)
|
CuSuite *get_reports_suite(void)
|
||||||
{
|
{
|
||||||
CuSuite *suite = CuSuiteNew();
|
CuSuite *suite = CuSuiteNew();
|
||||||
SUITE_ADD_TEST(suite, test_reorder_units);
|
SUITE_ADD_TEST(suite, test_reorder_units);
|
||||||
SUITE_ADD_TEST(suite, test_seen_faction);
|
SUITE_ADD_TEST(suite, test_seen_faction);
|
||||||
SUITE_ADD_TEST(suite, test_regionid);
|
SUITE_ADD_TEST(suite, test_regionid);
|
||||||
|
SUITE_ADD_TEST(suite, test_write_spaces);
|
||||||
|
SUITE_ADD_TEST(suite, test_write_many_spaces);
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue