static FILE* out;
+static void print_char_file(const char c)
+{
+ fputc(c, out);
+}
+
static void print_string_file(const char *str)
{
fputs(str, out);
vfprintf(out, format, ap);
}
-static void print_char_file(wchar_rep_t c)
-{
- const unsigned tc = (unsigned) c;
- if (tc < 0x80) {
- fputc(tc, out);
- } else if (tc < 0x800) {
- fputc(0xC0 | (tc >> 6), out);
- fputc(0x80 | (tc & 0x3F), out);
- } else if (tc < 0x10000) {
- fputc(0xE0 | ( tc >> 12), out);
- fputc(0x80 | ((tc >> 6) & 0x3F), out);
- fputc(0x80 | ( tc & 0x3F), out);
- } else {
- fputc(0xF0 | ( tc >> 18), out);
- fputc(0x80 | ((tc >> 12) & 0x3F), out);
- fputc(0x80 | ((tc >> 6) & 0x3F), out);
- fputc(0x80 | ( tc & 0x3F), out);
- }
-}
-
void print_to_file(FILE *new_out)
{
out = new_out;
static struct obstack *obst;
+static void print_char_obstack(const char c)
+{
+ obstack_1grow(obst, c);
+}
+
static void print_string_obstack(const char *str)
{
size_t len = strlen(str);
obstack_vprintf(obst, format, ap);
}
-static void print_char_obstack(wchar_rep_t c)
-{
- const unsigned tc = (unsigned) c;
- if (tc < 0x80) {
- obstack_1grow(obst, tc);
- } else if (tc < 0x800) {
- obstack_1grow(obst, 0xC0 | (tc >> 6));
- obstack_1grow(obst, 0x80 | (tc & 0x3F));
- } else if (tc < 0x10000) {
- obstack_1grow(obst, 0xE0 | ( tc >> 12));
- obstack_1grow(obst, 0x80 | ((tc >> 6) & 0x3F));
- obstack_1grow(obst, 0x80 | ( tc & 0x3F));
- } else {
- obstack_1grow(obst, 0xF0 | ( tc >> 18));
- obstack_1grow(obst, 0x80 | ((tc >> 12) & 0x3F));
- obstack_1grow(obst, 0x80 | ((tc >> 6) & 0x3F));
- obstack_1grow(obst, 0x80 | ( tc & 0x3F));
- }
-}
-
void print_to_obstack(struct obstack *new_obst)
{
obst = new_obst;
static char *buffer_pos;
static char *buffer_end;
-static void print_string_buffer(const char *str)
-{
- for (const char *c = str; *c != '\0'; ++c) {
- if (buffer_pos == buffer_end) {
- return;
- }
- *buffer_pos++ = *c;
- }
-}
-
-static void print_vformat_buffer(const char *format, va_list ap)
-{
- size_t size = buffer_end - buffer_pos + 1;
- int written = vsnprintf(buffer_pos, size, format, ap);
- buffer_pos += written;
-}
-
-static inline void buffer_add_char(int c)
+static void print_char_buffer(const char c)
{
if (buffer_pos == buffer_end)
return;
*buffer_pos++ = c;
}
-static void print_char_buffer(wchar_rep_t c)
+static void print_string_buffer(const char *str)
{
- const unsigned tc = (unsigned) c;
- if (tc < 0x80) {
- buffer_add_char(tc);
- } else if (tc < 0x800) {
- buffer_add_char(0xC0 | (tc >> 6));
- buffer_add_char(0x80 | (tc & 0x3F));
- } else if (tc < 0x10000) {
- buffer_add_char(0xE0 | ( tc >> 12));
- buffer_add_char(0x80 | ((tc >> 6) & 0x3F));
- buffer_add_char(0x80 | ( tc & 0x3F));
- } else {
- buffer_add_char(0xF0 | ( tc >> 18));
- buffer_add_char(0x80 | ((tc >> 12) & 0x3F));
- buffer_add_char(0x80 | ((tc >> 6) & 0x3F));
- buffer_add_char(0x80 | ( tc & 0x3F));
+ for (const char *c = str; *c != '\0'; ++c) {
+ print_char_buffer(*c);
}
}
+static void print_vformat_buffer(const char *format, va_list ap)
+{
+ size_t size = buffer_end - buffer_pos;
+ size_t written = (size_t) vsnprintf(buffer_pos, size, format, ap);
+ buffer_pos += written < size ? written : size;
+}
+
void print_to_buffer(char *buffer, size_t buffer_size)
{
buffer_pos = buffer;
- buffer_end = buffer + buffer_size - 1;
+ buffer_end = buffer + buffer_size - 2;
print_string = print_string_buffer;
print_vformat = print_vformat_buffer;
void (*print_string)(const char *str) = print_string_file;
void (*print_vformat)(const char *format, va_list ap) = print_vformat_file;
-void (*print_char)(wchar_rep_t c) = print_char_file;
+void (*print_char)(const char c) = print_char_file;
void printer_push(void)
{