Browse Source

Merge pull request #412 from dhylands/pfenv-strn-stack

Pfenv strn stack
pull/414/head
Damien George 11 years ago
parent
commit
e8e8f212aa
  1. 23
      py/pfenv.c

23
py/pfenv.c

@ -16,9 +16,8 @@
#include "formatfloat.h" #include "formatfloat.h"
#endif #endif
#define PF_PAD_SIZE 16 static const char pad_spaces[] = " ";
static const char *pad_spaces = " "; static const char pad_zeroes[] = "0000000000000000";
static const char *pad_zeroes = "0000000000000000";
void pfenv_vstr_add_strn(void *data, const char *str, unsigned int len){ void pfenv_vstr_add_strn(void *data, const char *str, unsigned int len){
vstr_add_strn(data, str, len); vstr_add_strn(data, str, len);
@ -28,16 +27,20 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
int left_pad = 0; int left_pad = 0;
int right_pad = 0; int right_pad = 0;
int pad = width - len; int pad = width - len;
char pad_fill[PF_PAD_SIZE]; int pad_size;
const char *pad_chars; const char *pad_chars;
if (!fill || fill == ' ' ) { if (!fill || fill == ' ' ) {
pad_chars = pad_spaces; pad_chars = pad_spaces;
pad_size = sizeof(pad_spaces) - 1;
} else if (fill == '0') { } else if (fill == '0') {
pad_chars = pad_zeroes; pad_chars = pad_zeroes;
pad_size = sizeof(pad_zeroes) - 1;
} else { } else {
memset(pad_fill, fill, PF_PAD_SIZE); // Other pad characters are fairly unusual, so we'll take the hit
pad_chars = pad_fill; // and output them 1 at a time.
pad_chars = &fill;
pad_size = 1;
} }
if (flags & PF_FLAG_CENTER_ADJUST) { if (flags & PF_FLAG_CENTER_ADJUST) {
@ -52,8 +55,8 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
if (left_pad) { if (left_pad) {
while (left_pad > 0) { while (left_pad > 0) {
int p = left_pad; int p = left_pad;
if (p > PF_PAD_SIZE) if (p > pad_size)
p = PF_PAD_SIZE; p = pad_size;
pfenv->print_strn(pfenv->data, pad_chars, p); pfenv->print_strn(pfenv->data, pad_chars, p);
left_pad -= p; left_pad -= p;
} }
@ -62,8 +65,8 @@ int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, in
if (right_pad) { if (right_pad) {
while (right_pad > 0) { while (right_pad > 0) {
int p = right_pad; int p = right_pad;
if (p > PF_PAD_SIZE) if (p > pad_size)
p = PF_PAD_SIZE; p = pad_size;
pfenv->print_strn(pfenv->data, pad_chars, p); pfenv->print_strn(pfenv->data, pad_chars, p);
right_pad -= p; right_pad -= p;
} }

Loading…
Cancel
Save