alt_printf 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ #ifndef PARSEF_H_
2
+ #define PARSEF_H_
3
+ #include "fmte.h"
4
+ #include "syntax.h"
5
+ #include "log.h"
6
+
7
+ void get_longarg(wchar_t **, wchar_t **, wchar_t, size_t *);
8
+ struct fmte *parsef(wchar_t **);
9
+
10
+ #endif
@@ -1,11 +1,4 @@
1
- #define _XOPEN_SOURCE
2
- #include <locale.h>
3
- #include <limits.h>
4
1
  #include "strbuf.h"
5
- #include "log.h"
6
-
7
- #define STRBUF_INI_SIZE 5
8
- #define STRBUF_GROW_STEP 100
9
2
 
10
3
  extern struct lconv *locale_info;
11
4
 
@@ -13,14 +6,14 @@ struct strbuf *strbuf_new() {
13
6
  struct strbuf *sb = malloc(sizeof(struct strbuf));
14
7
 
15
8
  if (NULL == sb) {
16
- LOG("can't alloc memory for new strbuf", NULL);
9
+ LOG("can't alloc memory for new strbuf\n");
17
10
  exit(1);
18
11
  }
19
12
 
20
13
  sb->start = sb->end = calloc(STRBUF_INI_SIZE, sizeof(wchar_t));
21
14
 
22
15
  if (NULL == sb->start) {
23
- LOG("can't alloc memory for new strbuf string", NULL);
16
+ LOG("can't alloc memory for new strbuf string\n");
24
17
  exit(1);
25
18
  }
26
19
 
@@ -74,14 +67,13 @@ void strbuf_append_strbuf(struct strbuf *sb, void *sbuf)
74
67
  }
75
68
  }
76
69
 
77
- void strbuf_appendw_strbuf(struct strbuf *sb, void *sbuf, long w)
78
- {
70
+ void strbuf_appendw_strbuf(struct strbuf *sb, void *sbuf, long w) {
79
71
  wchar_t *pos;
80
72
  long ws = 0;
81
73
  struct strbuf *frm = sbuf;
82
74
  LOG("frm->start: %p | frm->end: %p\n", frm->start, frm->end);
83
75
 
84
- for (pos = frm->start;pos<=frm->end;pos++) {
76
+ for (pos = frm->start; pos<=frm->end; pos++) {
85
77
  ws += wcwidth(*pos);
86
78
  LOG("new width would be: %ld, requested width: %ld\n", ws, w);
87
79
  if (ws > w) break;
@@ -89,21 +81,30 @@ void strbuf_appendw_strbuf(struct strbuf *sb, void *sbuf, long w)
89
81
  }
90
82
  }
91
83
 
92
- void strbuf_append_char(struct strbuf *sb, void *chr)
93
- {
84
+ void strbuf_append_char(struct strbuf *sb, void *chr) {
94
85
  wint_t *c = chr;
95
86
  strbuf_append(sb, *c);
96
87
  }
97
88
 
98
- void strbuf_append_str(struct strbuf *sb, void *str, int maxwidth)
99
- {
89
+ void strbuf_append_str(struct strbuf *sb, void *str, int maxwidth) {
100
90
  wchar_t *s = str;
101
91
  wchar_t *end = &s[wcslen(s)];
102
92
  int width = 0;
93
+ int maxlen = -1;
94
+
95
+ if (maxwidth < 0) {
96
+ maxlen = maxwidth * -1;
97
+ }
103
98
 
104
99
  for (;s<end;s++) {
105
- width += wcwidth(*s);
106
- if (width > maxwidth) return;
100
+ if (maxlen >= 0) {
101
+ width++;
102
+ if (width > maxlen) return;
103
+ } else {
104
+ width += wcwidth(*s);
105
+ if (width > maxwidth) return;
106
+ }
107
+
107
108
  strbuf_append(sb, *s);
108
109
  }
109
110
  }
@@ -111,20 +112,37 @@ void strbuf_append_str(struct strbuf *sb, void *str, int maxwidth)
111
112
  void strbuf_append_int(struct strbuf *sb, void *in)
112
113
  {
113
114
  long int *i = in;
114
- wchar_t wcs[50];
115
- swprintf(wcs, 50, L"%ld", *i);
116
- strbuf_append_str(sb, wcs, 50);
115
+ wchar_t wcs[TMPLEN];
116
+ long len = swprintf(wcs, TMPLEN - 1, L"%ld", *i);
117
+ if (len < 0) {
118
+ strbuf_append_str(sb, L"error adding int", 16);
119
+ } else {
120
+ strbuf_append_str(sb, wcs, -1 * len);
121
+ }
117
122
  }
118
123
 
119
124
  void strbuf_append_double(struct strbuf *sb, void *dub, int prec)
120
125
  {
121
126
  double *d = dub;
122
- wchar_t wcs[50];
123
- wchar_t format[30];
124
- swprintf(format, 30, L"%%.%ldf", prec);
127
+ wchar_t wcs[TMPLEN];
128
+ wchar_t format[TMPLEN];
129
+ int rprec = prec;
130
+
131
+ if (rprec > MAXPREC) rprec = MAXPREC;
132
+
133
+ swprintf(format, TMPLEN - 1, L"%%.%ldf", rprec);
134
+
125
135
  LOG("format: %ls\n", format);
126
- swprintf(wcs, 50, format, *d);
127
- strbuf_append_str(sb, wcs, 50);
136
+
137
+ long len = swprintf(wcs, TMPLEN - 1, format, *d);
138
+ if (len < 0) {
139
+ strbuf_append_str(sb, L"error adding float", 18);
140
+ } else {
141
+ LOG("inserting double: len: %ld\n", len);
142
+ strbuf_append_str(sb, wcs, len);
143
+
144
+ if (rprec < prec) strbuf_pad(sb, L'0', rprec - prec);
145
+ }
128
146
  }
129
147
 
130
148
  void strbuf_pad(struct strbuf *sb, wchar_t pc, int amnt)
@@ -1,10 +1,19 @@
1
1
  #ifndef STRBUF_H
2
2
  #define STRBUF_H
3
+
4
+ #ifndef _XOPEN_SOURCE
5
+ #define _XOPEN_SOURCE
6
+ #endif
7
+
3
8
  #include <wchar.h>
4
- #include <stdio.h>
5
9
  #include <stdlib.h>
6
- #include <string.h>
7
- #include <math.h>
10
+ #include <stdio.h>
11
+ #include "log.h"
12
+
13
+ #define STRBUF_INI_SIZE 5
14
+ #define STRBUF_GROW_STEP 100
15
+ #define TMPLEN 50
16
+ #define MAXPREC 25
8
17
 
9
18
  struct strbuf *strbuf_new();
10
19
  void strbuf_destroy(struct strbuf *sb);
@@ -16,14 +16,17 @@
16
16
  #define FS_T_TERN '?'
17
17
  #define FS_T_ALIGN '='
18
18
 
19
- #define FS_A_STRINGSTART '('
20
- #define FS_A_STRINGEND ')'
19
+ #define FS_A_PARENARG_S '('
20
+ #define FS_A_PARENARG_E ')'
21
+ #define FS_A_ANGLEARG_S '<'
22
+ #define FS_A_ANGLEARG_E '>'
23
+
21
24
  #define FS_A_CHARARG '~'
22
25
  #define FS_A_LALIGN '-'
26
+ #define FS_A_CALIGN '^'
23
27
  #define FS_A_SPAD ' '
24
-
25
- #define FS_A_RBHASHSTART '<'
26
- #define FS_A_RBHASHEND '>'
28
+ #define FS_A_ZPAD '0'
29
+ #define FS_A_PREC '.'
27
30
 
28
31
  #define FS_D_PREC 100
29
32
 
Binary file
@@ -1,3 +1,3 @@
1
1
  module AltPrintf
2
- VERSION = [0, 1, 3]
2
+ VERSION = [0, 1, 4]
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alt_printf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stone Tickle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-11 00:00:00.000000000 Z
11
+ date: 2019-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -50,14 +50,20 @@ files:
50
50
  - Rakefile
51
51
  - alt_printf.gemspec
52
52
  - ext/alt_printf/alt_printf.c
53
- - ext/alt_printf/altprintf.c
54
53
  - ext/alt_printf/altprintf.h
54
+ - ext/alt_printf/enums.h
55
55
  - ext/alt_printf/extconf.h
56
56
  - ext/alt_printf/extconf.rb
57
+ - ext/alt_printf/extconf_debug.rb
57
58
  - ext/alt_printf/extconf_dev.rb
58
- - ext/alt_printf/list.c
59
- - ext/alt_printf/list.h
59
+ - ext/alt_printf/extconf_helper.rb
60
+ - ext/alt_printf/fmt.c
61
+ - ext/alt_printf/fmt.h
62
+ - ext/alt_printf/fmte.c
63
+ - ext/alt_printf/fmte.h
60
64
  - ext/alt_printf/log.h
65
+ - ext/alt_printf/parsef.c
66
+ - ext/alt_printf/parsef.h
61
67
  - ext/alt_printf/strbuf.c
62
68
  - ext/alt_printf/strbuf.h
63
69
  - ext/alt_printf/syntax.h
@@ -76,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
82
  requirements:
77
83
  - - ">="
78
84
  - !ruby/object:Gem::Version
79
- version: 2.6.3
85
+ version: 2.5.5
80
86
  required_rubygems_version: !ruby/object:Gem::Requirement
81
87
  requirements:
82
88
  - - ">="
@@ -1,250 +0,0 @@
1
- #include "syntax.h"
2
- #include "altprintf.h"
3
- #include "log.h"
4
-
5
- void default_format(struct format *f) {
6
- struct width width = {.prec = -1, .pad = 0};
7
- f->stringarg_start = NULL;
8
- f->stringarg_end = NULL;
9
- f->chararg = L':';
10
- f->padchar = L' ';
11
- f->align = Right;
12
- f->width = width;
13
- f->le = NULL;
14
- }
15
-
16
- void format_mul(struct strbuf *sb, struct format *f)
17
- {
18
- long int *i = f->le->data;
19
- strbuf_pad(sb, f->chararg, *i);
20
- }
21
- void format_tern(struct strbuf *sb, struct format *f)
22
- {
23
- long int *b = f->le->data;
24
- int first_half = 1;
25
- wchar_t sep = f->chararg;
26
- wchar_t *p = f->stringarg_start;
27
- for (;p<=f->stringarg_end;p++) {
28
- LOG("*p: %lc, first half? %d, bool: %ld, sep: %lc\n", (wint_t)*p, first_half, *b, (wint_t)sep);
29
- if (*p == sep) first_half = 0;
30
- else if (*b && first_half) strbuf_append_char(sb, p);
31
- else if (!*b && !first_half) strbuf_append_char(sb, p);
32
- }
33
- }
34
-
35
- void format_string(struct strbuf *sb, struct format *f)
36
- {
37
- int prec = f->width.prec == -1 ? 100000000 : f->width.prec;
38
- strbuf_append_str(sb, f->le->data, prec);
39
- }
40
- void format_char(struct strbuf *sb, struct format *f)
41
- {
42
- strbuf_append_char(sb, f->le->data);
43
- }
44
- void format_int(struct strbuf *sb, struct format *f)
45
- {
46
- strbuf_append_int(sb, f->le->data);
47
- }
48
- void format_double(struct strbuf *sb, struct format *f)
49
- {
50
- int prec = f->width.prec == -1 ? 3 : f->width.prec;
51
- strbuf_append_double(sb, f->le->data, prec);
52
- }
53
-
54
- void format(struct strbuf *sb, struct format *f, void (*to_s)(struct strbuf *, struct format *))
55
- {
56
- struct strbuf *tmp = strbuf_new();
57
- to_s(tmp, f);
58
-
59
- if (tmp->len == 0) return;
60
-
61
- int pad = f->width.pad - tmp->width;
62
-
63
- if (pad > 0) {
64
- LOG("padding: %d\n", pad);
65
- switch(f->align) {
66
- case Right:
67
- strbuf_append_strbuf(sb, tmp);
68
- strbuf_pad(sb, f->padchar, pad);
69
- break;
70
- case Left:
71
- strbuf_pad(sb, f->padchar, pad);
72
- strbuf_append_strbuf(sb, tmp);
73
- break;
74
- case Center:
75
- strbuf_pad(sb, f->padchar, pad/2);
76
- strbuf_append_strbuf(sb, tmp);
77
- strbuf_pad(sb, f->padchar, pad/2 + pad%2);
78
- break;
79
- }
80
- } else {
81
- strbuf_append_strbuf(sb, tmp);
82
- }
83
-
84
- strbuf_destroy(tmp);
85
- }
86
-
87
- wchar_t *altsprintf(wchar_t *fmt, struct list_elem *le) {
88
- int lvl = 0;
89
- int split = 0;
90
- struct strbuf *sbs[] = {strbuf_new(), strbuf_new()};
91
- struct strbuf *sb = sbs[0];
92
- wchar_t *end = &fmt[wcslen(fmt)];
93
- wchar_t *jump;
94
-
95
- void (*append_func)(struct strbuf *, struct format *);
96
- struct format f;
97
- long int *number_p = NULL;
98
- long int *width;
99
- wint_t split_pad = L' ';
100
-
101
- for (;fmt<end;fmt++) {
102
- LOG("checking char '%lc', lvl: '%d'\n", (wint_t)(*fmt), lvl);
103
- switch (lvl) {
104
- case 0:
105
- switch(*fmt) {
106
- case FS_START:
107
- default_format(&f);
108
- lvl = 1;
109
- break;
110
- case FS_ESC:
111
- lvl = 3;
112
- break;
113
- default:
114
- strbuf_append(sb, *fmt);
115
- break;
116
- }; break;
117
- case 1:
118
- switch(*fmt) {
119
- /* special arguments */
120
- case FS_A_RBHASHSTART:
121
- while (fmt < end && *fmt != FS_A_RBHASHEND) fmt++;
122
- break;
123
- case FS_A_STRINGSTART:
124
- f.stringarg_start = fmt + 1;
125
- lvl = 2;
126
- break;
127
- case FS_A_CHARARG:
128
- f.chararg = *(fmt+1);
129
- fmt += 1;
130
- break;
131
-
132
- /* standard arguments */
133
- case FS_A_LALIGN:
134
- f.align = Left;
135
- break;
136
- case FS_A_SPAD:
137
- f.padchar = FS_A_SPAD;
138
- break;
139
- case 0:
140
- f.padchar = '0';
141
- break;
142
- case '.':
143
- number_p = &f.width.prec;
144
- fmt++;
145
- case '1': case '2': case '3': case '4': case '5':
146
- case '6': case '7': case '8': case '9':
147
- if (number_p == NULL) number_p = &f.width.pad;
148
- *number_p = wcstol(fmt, &jump, 10);
149
- fmt = (jump-1);
150
- break;
151
-
152
- /* align operator */
153
- case FS_T_ALIGN:
154
- if (le != NULL && le->type != Null) {
155
- width = le->data;
156
- le = le->next;
157
- } else {
158
- goto no_more_args;
159
- }
160
-
161
- split = 1;
162
- split_pad = f.chararg;
163
- sb = sbs[1];
164
- lvl = 0;
165
- break;
166
-
167
- /* types */
168
- case FS_T_STRING:
169
- append_func = format_string;
170
- goto match;
171
- case FS_T_TERN:
172
- append_func = format_tern;
173
- goto match;
174
- case FS_T_INT:
175
- append_func = format_int;
176
- goto match;
177
- case FS_T_MUL:
178
- append_func = format_mul;
179
- goto match;
180
- case FS_T_CHAR:
181
- append_func = format_char;
182
- goto match;
183
- case FS_T_DOUBLE:
184
- append_func = format_double;
185
- match:
186
- if (le != NULL && le->type != Null) {
187
- f.le = le;
188
- format(sb, &f, append_func);
189
- le = le->next;
190
- }
191
- lvl = 0;
192
- break;
193
- case FS_START:
194
- strbuf_append(sb, FS_START);
195
- lvl = 0;
196
- break;
197
- default:
198
- lvl = 0;
199
- break;
200
- }; break;
201
- case 2:
202
- if (*fmt == FS_A_STRINGEND) {
203
- f.stringarg_end = fmt - 1;
204
- lvl = 1;
205
- }; break;
206
- case 3:
207
- switch(*fmt) {
208
- case FS_ESC_NL:
209
- strbuf_append(sb, '\n');
210
- break;
211
- case FS_ESC_ESC:
212
- strbuf_append(sb, '\e');
213
- break;
214
- default:
215
- strbuf_append(sb, *fmt);
216
- break;
217
- };
218
- lvl = 0;
219
- break;
220
- }
221
- }
222
-
223
- wchar_t *str;
224
- no_more_args:
225
- if (split) {
226
- LOG("splitting string\n");
227
- lvl = *width - (sbs[0]->width + sbs[1]->width);
228
- if (lvl >= 0) {
229
- LOG("padding center\n");
230
- strbuf_pad(sbs[0], split_pad, lvl);
231
- strbuf_append_strbuf(sbs[0], sbs[1]);
232
- } else if (sbs[0]->width > *width) {
233
- LOG("the first half is longer than the requested with\n");
234
- strbuf_destroy(sbs[1]);
235
- sbs[1] = sbs[0];
236
- sbs[0] = strbuf_new();
237
- strbuf_appendw_strbuf(sbs[0], sbs[1], *width);
238
- } else {
239
- LOG("just shave some off the last half\n");
240
- strbuf_appendw_strbuf(sbs[0], sbs[1], *width - sbs[0]->width);
241
- }
242
- }
243
-
244
- str = strbuf_cstr(sbs[0]);
245
-
246
- strbuf_destroy(sbs[0]);
247
- strbuf_destroy(sbs[1]);
248
-
249
- return str;
250
- }