altprintf 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df338e1fe04766f80321b04dc0380e6c022ae06c8e4d13cbaea05522630b7463
4
- data.tar.gz: 9b7c74147e5ef03e947467d2c4c89e7471c873a56410d2aa00f4015680352c96
3
+ metadata.gz: 30fe21097d8d96fc45f13bf03bc0c66c73cabd30e27d9a5829d33022e54d21fb
4
+ data.tar.gz: 2e3974374ac51f2f6750cdf48b660711ac9c4de60c14a9ba3c239a345cf9541d
5
5
  SHA512:
6
- metadata.gz: ba342f2d892020efc12b7e0489a376b005fb2161b81ae235ac9021d04f7d594c9bda20e0c8c1a332a607a4897688faa34143c2bd1dcb100f6b71611992eeaa30
7
- data.tar.gz: c19648ebefd35dbdf6b00760e92567be3f42c042227ba3e49d32f9595dcec29ce67ca47169c2f64ca1c164b199e3050c8a912403a1a28855c89b54084026a49d
6
+ metadata.gz: 5dfa57f474993381bd7c9c5704b09a867d02795abaa10f6fda6bc6a64a76b6c970bc80af41f225ef6dd98b610013b75eb2b101661dddea7e7407f06f3dc21bb2
7
+ data.tar.gz: 95b00d639a18eaccb25105c80129cab7f651c7f3cb5bcc0836177c06ad0170548e8abd6849e48efed84fca54c1750b411f5f736c87e4e1940488e41a1d5c5b67
data/altprintf.gemspec CHANGED
@@ -17,8 +17,10 @@ Altprintf::SPEC = Gem::Specification.new do |s|
17
17
  s.extensions = Dir["ext/**/extconf.rb"]
18
18
  s.require_paths = ['lib']
19
19
 
20
- s.required_ruby_version = '>= 2.5.5'
20
+ s.required_ruby_version = '>= 2.5.7'
21
21
 
22
+ s.add_development_dependency 'benchmark-ips', '~> 2.7'
22
23
  s.add_development_dependency 'rake-compiler', '~> 1.0'
23
24
  s.add_development_dependency 'rake', '~> 12.3'
25
+ s.add_development_dependency 'rspec', '~> 3.9'
24
26
  end unless Altprintf.const_defined?(:'SPEC')
@@ -0,0 +1,8 @@
1
+ #ifndef ALTPRINTF_H_
2
+ #define ALTPRINTF_H_
3
+ #define ALTPRINTF_VERSION "0.3.0"
4
+
5
+ #include <altprintf/fmt.h>
6
+ #include <altprintf/fmte.h>
7
+ #include <altprintf/parsef.h>
8
+ #endif
@@ -0,0 +1,6 @@
1
+ #ifndef _CWIDTH_H
2
+ #define _CWIDTH_H
3
+ #include <stdlib.h>
4
+ int cswidth(const char *utf8, size_t maxlen);
5
+ int cwidth(const char* utf8);
6
+ #endif
@@ -1,5 +1,5 @@
1
- #ifndef ARGTYPE_H_
2
- #define ARGTYPE_H_
1
+ #ifndef _ALTPRINTF_ENUM_H
2
+ #define _ALTPRINTF_ENUM_H
3
3
  enum arg_type { FMul, FTern, FAlign, FInt, FChar, FDouble, FString, FRaw, FNone,
4
4
  FEnd };
5
5
  enum align { Left, Right, Center };
@@ -11,5 +11,4 @@ enum altprintf_err {
11
11
  };
12
12
 
13
13
  extern enum altprintf_err apf_err;
14
-
15
14
  #endif
@@ -0,0 +1,5 @@
1
+ #ifndef FMT_H_
2
+ #define FMT_H_
3
+ #include <altprintf/fmte.h>
4
+ char *assemble_fmt(struct fmte *);
5
+ #endif
@@ -1,23 +1,20 @@
1
1
  #ifndef FMTE_H_
2
2
  #define FMTE_H_
3
- #include <wchar.h>
4
3
  #include <stdlib.h>
5
- #include <stdio.h>
6
- #include "enums.h"
7
- #include "log.h"
4
+ #include <altprintf/enums.h>
8
5
 
9
6
  struct fmte {
10
- wchar_t *parenarg_start;
11
- wchar_t *parenarg_end;
7
+ const char *parenarg_start;
8
+ const char *parenarg_end;
12
9
  size_t parenarg_len;
13
10
 
14
- wchar_t *anglearg_start;
15
- wchar_t *anglearg_end;
11
+ const char *anglearg_start;
12
+ const char *anglearg_end;
16
13
  size_t anglearg_len;
17
14
 
18
- wint_t chararg;
15
+ char chararg;
19
16
 
20
- wint_t padchar;
17
+ char padchar;
21
18
 
22
19
  enum arg_type type;
23
20
  enum align align;
@@ -30,7 +27,7 @@ struct fmte {
30
27
  struct fmte *next;
31
28
  };
32
29
 
33
- struct fmte *fmte_ini();
30
+ struct fmte *fmte_ini(void);
34
31
  void fmte_inspect(struct fmte *);
35
32
  void fmte_push(struct fmte *, struct fmte *);
36
33
  void fmte_destroy(struct fmte *);
File without changes
@@ -0,0 +1,6 @@
1
+ #ifndef PARSEF_H_
2
+ #define PARSEF_H_
3
+ #include <stdlib.h>
4
+
5
+ struct fmte *parsef(const char **);
6
+ #endif
@@ -0,0 +1,25 @@
1
+ #ifndef STRBUF_H
2
+ #define STRBUF_H
3
+ #include <stdlib.h>
4
+
5
+ struct strbuf *strbuf_new(void);
6
+ void strbuf_destroy(struct strbuf *sb);
7
+ void strbuf_append(struct strbuf *sb, char);
8
+ void strbuf_append_char(struct strbuf *sb, const void *);
9
+ void strbuf_append_str(struct strbuf *, const void *, int);
10
+ void strbuf_append_int(struct strbuf *sb, const void *);
11
+ void strbuf_append_double(struct strbuf *, const void *, int);
12
+ void strbuf_append_strbuf(struct strbuf *, const void *);
13
+ void strbuf_appendw_strbuf(struct strbuf *, const void *, long);
14
+ void strbuf_pad(struct strbuf *, char, int);
15
+ char *strbuf_cstr(struct strbuf *);
16
+ size_t strbuf_width(struct strbuf *);
17
+
18
+ struct strbuf {
19
+ char *start;
20
+ char *end;
21
+ size_t len;
22
+ size_t width;
23
+ size_t cap;
24
+ };
25
+ #endif
@@ -4,7 +4,7 @@
4
4
  #define FS_START '%'
5
5
  #define FS_DOUBLE_SEP '.'
6
6
 
7
- #define FS_ESC L'\\'
7
+ #define FS_ESC '\\'
8
8
  #define FS_ESC_NL 'n'
9
9
  #define FS_ESC_ESC 'e'
10
10
 
@@ -24,12 +24,11 @@
24
24
  #define FS_A_CHARARG '~'
25
25
  #define FS_A_LALIGN '-'
26
26
  #define FS_A_CALIGN '^'
27
- #define FS_A_SPAD ' '
28
- #define FS_A_ZPAD '0'
27
+ #define FS_A_PAD '_'
29
28
  #define FS_A_PREC '.'
30
29
 
31
30
  #define FS_D_PREC 100
32
31
 
33
- #define EOS L'\0'
32
+ #define EOS '\0'
34
33
 
35
34
  #endif
data/ext/altprintf/ext.c CHANGED
@@ -9,56 +9,40 @@
9
9
  #include "extconf.h"
10
10
  #include <stdio.h>
11
11
  #include <locale.h>
12
- #include <wchar.h>
13
12
  #include <ruby.h>
14
13
  #include <ruby/encoding.h>
15
- #include "altprintf.h"
16
- #include "log.h"
14
+ #include <altprintf/altprintf.h>
15
+ #include <altprintf/log.h>
17
16
 
18
17
  #define MODNAME "Altprintf"
19
18
 
20
19
  rb_encoding *enc;
21
20
 
22
- wchar_t *rbstowcs(VALUE str)
21
+ char *rbstocs(VALUE *rbstr)
23
22
  {
24
- const char *cstr;
25
- wchar_t *wstr;
26
- size_t len;
27
-
28
- cstr = StringValueCStr(str);
29
-
30
- len = mbsrtowcs(NULL, &cstr, 0, NULL);
31
- wstr = calloc(len + 1, sizeof(wchar_t));
32
- len = mbsrtowcs(wstr, &cstr, len, NULL);
23
+ char *p, *tmp;
24
+ long len;
33
25
 
34
- LOG("rbs to wcs, len: %d, cstr: '%s'\n", len, cstr);
35
- LOG("wide string: '%ls'\n", wstr);
26
+ p = StringValuePtr(*rbstr);
27
+ len = RSTRING_LEN(*rbstr);
28
+ tmp = calloc(len + 1, sizeof(char));
29
+ strncpy(tmp, p, len);
30
+ tmp[len] = '\0';
36
31
 
37
- return wstr;
32
+ return tmp;
38
33
  }
39
34
 
40
- VALUE wcstorbs(const wchar_t *wstr)
35
+ VALUE cstorbs(const char *cstr)
41
36
  {
42
37
  size_t len;
43
- char *cstr;
44
38
  VALUE str;
45
39
 
46
- LOG("converting wcs to rbs\n");
47
- LOG("wcs: '%ls'\n\n", wstr);
48
-
49
- len = wcsrtombs(NULL, &wstr, 0, NULL);
50
- LOG("len: %d\n", len);
51
- cstr = calloc(len, sizeof(wchar_t));
52
- wcsrtombs(cstr, &wstr, len, NULL);
53
- LOG("cstr: '%s'\n", cstr);
54
-
55
- LOG("wcs to rbs, len: %d, wcs: %ls, mbs: '%s'\n", len, wstr, cstr);
40
+ len = strlen(cstr);
56
41
 
57
42
  if (len == (size_t)-1)
58
43
  return Qnil;
59
44
 
60
45
  str = rb_external_str_new_with_enc(cstr, len, enc);
61
- free(cstr);
62
46
 
63
47
  return str;
64
48
  }
@@ -66,9 +50,6 @@ VALUE wcstorbs(const wchar_t *wstr)
66
50
  VALUE get_entry(struct fmte *f, size_t argc, size_t *argi, VALUE *argv, VALUE *hash)
67
51
  {
68
52
  VALUE sym, entry;
69
- size_t len;
70
- const wchar_t *tmpw;
71
- char *cstr;
72
53
 
73
54
  LOG("getting entry\n");
74
55
 
@@ -81,35 +62,22 @@ VALUE get_entry(struct fmte *f, size_t argc, size_t *argi, VALUE *argv, VALUE *h
81
62
  return entry;
82
63
  }
83
64
 
84
- LOG("getting from hash\n");
85
-
86
- tmpw = f->anglearg_start;
87
- len = wcsnrtombs(NULL, &tmpw, f->anglearg_len, 0, NULL);
88
- LOG("allocating %d, maxlen %d\n", len, f->anglearg_len);
89
-
90
- cstr = calloc(len + 1, sizeof(char));
91
- wcsnrtombs(cstr, &tmpw, f->anglearg_len, len, NULL);
92
- LOG("wrote cstr %s\n", cstr);
93
-
94
- LOG("symbol | cstr: '%s', len %d\n", cstr, len);
95
-
96
- sym = rb_check_symbol_cstr(cstr, len, enc);
65
+ sym = rb_check_symbol_cstr(f->anglearg_start, f->anglearg_len, enc);
97
66
  entry = rb_hash_lookup2(*hash, sym, Qnil);
98
67
  if (entry == Qnil)
99
- rb_raise(rb_eKeyError, "missing key :%s", cstr);
100
- free(cstr);
68
+ rb_raise(rb_eKeyError, "missing key " PRIsVALUE, sym);
101
69
 
102
70
  return entry;
103
71
  }
104
72
 
105
- wchar_t *rb_apformat(wchar_t *fmt, size_t argc, size_t *argi, VALUE *argv, VALUE *hash)
73
+ char *rb_apformat(const char *fmt, size_t argc, size_t *argi, VALUE *argv, VALUE *hash)
106
74
  {
107
75
  struct fmte *f, *head;
108
- wchar_t *final;
76
+ char *final;
109
77
  int loop = 1;
110
78
  long *tmpi;
111
- wint_t *tmpc;
112
- wchar_t *tmps;
79
+ char *tmpc;
80
+ char *tmps;
113
81
  double *tmpd;
114
82
  void *tmp;
115
83
  VALUE entry;
@@ -131,7 +99,7 @@ wchar_t *rb_apformat(wchar_t *fmt, size_t argc, size_t *argi, VALUE *argv, VALUE
131
99
  case FString:
132
100
  Check_Type(entry, T_STRING);
133
101
 
134
- tmp = rbstowcs(entry);
102
+ tmp = rbstocs(&entry);
135
103
  goto match;
136
104
  case FTern:
137
105
  tmpi = malloc(sizeof(long));
@@ -152,9 +120,9 @@ wchar_t *rb_apformat(wchar_t *fmt, size_t argc, size_t *argi, VALUE *argv, VALUE
152
120
  case FChar:
153
121
  Check_Type(entry, T_STRING);
154
122
 
155
- tmpc = malloc(sizeof(wint_t));
156
- tmps = rbstowcs(entry);
157
- *tmpc = btowc(tmps[0]);
123
+ tmpc = malloc(sizeof(char));
124
+ tmps = StringValueCStr(entry);
125
+ *tmpc = tmps[0];
158
126
  tmp = tmpc;
159
127
  goto match;
160
128
  case FDouble:
@@ -188,6 +156,7 @@ match:
188
156
 
189
157
  LOG("got all fmt elements\n");
190
158
  final = assemble_fmt(head);
159
+ LOG("assembled fmt: %s", final);
191
160
  fmte_destroy(head);
192
161
  return final;
193
162
  }
@@ -195,9 +164,10 @@ match:
195
164
  VALUE rb_altprintf(long passes, size_t argc, VALUE *argv, VALUE self)
196
165
  {
197
166
  VALUE fmt, args, hash, final;
198
- wchar_t *wfmt;
199
- wchar_t *formatted;
167
+ char *wfmt;
168
+ char *formatted;
200
169
  size_t argi;
170
+ int free_wfmt;
201
171
 
202
172
  apf_err = apfe_none;
203
173
  rb_scan_args(argc, argv, "1*:", &fmt, &args, &hash);
@@ -211,24 +181,28 @@ VALUE rb_altprintf(long passes, size_t argc, VALUE *argv, VALUE self)
211
181
  if (passes == 0)
212
182
  return fmt;
213
183
 
214
- wfmt = rbstowcs(fmt);
184
+ free_wfmt = 0;
185
+ wfmt = StringValueCStr(fmt);
215
186
  formatted = NULL;
216
187
 
217
188
  argi = 0;
218
189
  for (; passes > 0; passes--) {
219
- LOG("wfmt: %ls\n", wfmt);
190
+ LOG("wfmt: %s\n", wfmt);
220
191
 
221
192
  formatted = rb_apformat(wfmt, argc, &argi, &args, &hash);
222
193
 
223
- LOG("formatted result: '%ls'\n", formatted);
194
+ LOG("formatted result: '%s'\n", formatted);
224
195
 
225
- free(wfmt);
196
+ if (free_wfmt)
197
+ free(wfmt);
198
+ else
199
+ free_wfmt = 1;
226
200
  wfmt = formatted;
227
201
  }
228
202
 
229
203
 
230
- LOG("final: '%ls'\n", formatted);
231
- final = wcstorbs(formatted);
204
+ LOG("final: '%s'\n", formatted);
205
+ final = cstorbs(formatted);
232
206
 
233
207
  free(formatted);
234
208
 
@@ -2,15 +2,23 @@ require 'mkmf'
2
2
 
3
3
  module ExtconfHelper
4
4
  BASE_DIR = File.join(__dir__, '../../../')
5
+ SRC_DIR = File.join(BASE_DIR, 'subprojects/libaltprintf/src/')
6
+ BUILD_DIR = File.join(BASE_DIR, 'build')
7
+ LIB_DIR = File.join(BUILD_DIR, 'subprojects/libaltprintf/')
8
+ INC_DIR = File.join(BASE_DIR, 'subprojects/libaltprintf/include')
5
9
 
6
10
  module_function
7
11
 
8
- def dev_header
9
- find_header('altprintf.h', File.join(BASE_DIR, 'src'))
10
- end
12
+ def dev_setup
13
+ unless find_header('altprintf/altprintf.h', INC_DIR)
14
+ $stderr.puts("couldn't find header 'altprintf.h'")
15
+ exit(1)
16
+ end
11
17
 
12
- def dev_objs(folder = 'release')
13
- $objs = Dir[File.join(BASE_DIR, "target/#{folder}/*.o")] + ['ext.o']
18
+ unless find_library('altprintf', 'parsef', LIB_DIR)
19
+ $stderr.puts("you haven't built libaltprintf yet")
20
+ exit(1)
21
+ end
14
22
  end
15
23
 
16
24
  def setup(mode = 'release')
@@ -19,12 +27,7 @@ module ExtconfHelper
19
27
  when 'release'
20
28
  # do nothing
21
29
  when 'dev'
22
- dev_header
23
- dev_objs('release')
24
- when 'debug'
25
- dev_header
26
- $defs.push("-DDEBUG")
27
- dev_objs('debug')
30
+ dev_setup
28
31
  else
29
32
  raise(ArgumentError, "invalid mode #{mode}")
30
33
  end
data/ext/altprintf/fmt.c CHANGED
@@ -1,8 +1,17 @@
1
- #include "fmt.h"
1
+ #include <altprintf/syntax.h>
2
+ #include <altprintf/strbuf.h>
3
+ #include <altprintf/fmte.h>
4
+ #include <altprintf/log.h>
5
+ #include <altprintf/fmt.h>
2
6
 
3
7
  #define BUFNUM 25
4
8
 
5
- #define CHECKNULL(p) if (p == NULL) { apf_err = apfe_missing_argument; return; }
9
+ #define CHECKNULL(p) do { \
10
+ if (p == NULL) { \
11
+ apf_err = apfe_missing_argument; \
12
+ return; \
13
+ } \
14
+ } while (0)
6
15
 
7
16
  enum altprintf_err apf_err;
8
17
 
@@ -27,10 +36,11 @@ void fmt_tern(struct strbuf *sb, struct fmte *f)
27
36
 
28
37
  long int *b = f->value;
29
38
  int first_half = 1;
30
- wchar_t sep = f->chararg;
31
- wchar_t *p = f->parenarg_start;
39
+ char sep = f->chararg;
40
+ const char *p = f->parenarg_start;
32
41
  for (; p <= f->parenarg_end; p++) {
33
- LOG("*p: %lc, first half? %d, bool: %ld, sep: %lc\n", (wint_t)*p, first_half, *b, (wint_t)sep);
42
+ LOG("*p: %lc, first half? %d, bool: %ld, sep: %lc\n",
43
+ (char)*p, first_half, *b, (char)sep);
34
44
  if (*p == sep)
35
45
  first_half = 0;
36
46
  else if (*b && first_half)
@@ -40,25 +50,25 @@ void fmt_tern(struct strbuf *sb, struct fmte *f)
40
50
  }
41
51
  }
42
52
 
43
- wchar_t process_escape_seq(wchar_t seq)
53
+ char process_escape_seq(char seq)
44
54
  {
45
55
  switch (seq) {
46
56
  case FS_ESC_NL:
47
- return L'\n';
57
+ return '\n';
48
58
  case FS_ESC_ESC:
49
- return L'\e';
59
+ return '\e';
50
60
  case 'a':
51
- return L'\a';
61
+ return '\a';
52
62
  case 'b':
53
- return L'\b';
63
+ return '\b';
54
64
  case 'f':
55
- return L'\f';
65
+ return '\f';
56
66
  case 'r':
57
- return L'\r';
67
+ return '\r';
58
68
  case 't':
59
- return L'\t';
69
+ return '\t';
60
70
  case 'v':
61
- return L'\v';
71
+ return '\v';
62
72
  default:
63
73
  return seq;
64
74
  }
@@ -68,15 +78,15 @@ void fmt_raw(struct strbuf *sb, struct fmte *f)
68
78
  {
69
79
  CHECKNULL(f->parenarg_start);
70
80
 
71
- wchar_t *p;
72
- wchar_t c;
81
+ const char *p;
82
+ char c;
73
83
  long i;
74
84
 
75
85
  p = f->parenarg_start;
76
86
  i = f->parenarg_len;
77
87
 
78
88
  for (; i > 0; i--) {
79
- LOG("p is '%lc' (%ld)\n", (wint_t)*p, (long)*p);
89
+ LOG("p is '%lc' (%ld)\n", (char)*p, (long)*p);
80
90
  c = (i > 1 && *p == FS_ESC) ? process_escape_seq(*++p) : *p;
81
91
 
82
92
  strbuf_append(sb, c);
@@ -150,14 +160,14 @@ void fmt(struct strbuf *sb, struct fmte *f, void (*fmtr)(struct strbuf *, struct
150
160
  strbuf_destroy(tmp);
151
161
  }
152
162
 
153
- wchar_t *assemble_fmt(struct fmte *head)
163
+ char *assemble_fmt(struct fmte *head)
154
164
  {
155
165
  struct fmte *f = head;
156
166
  struct strbuf *bufs[BUFNUM];
157
167
  struct fmte *splits[BUFNUM];
158
168
  size_t buf = 0, i;
159
169
  size_t w, tw, rw;
160
- wchar_t *final;
170
+ char *final;
161
171
 
162
172
  void (*fmtr)(struct strbuf *, struct fmte *) = NULL;
163
173
  int loop = 1;
data/ext/altprintf/fmte.c CHANGED
@@ -1,6 +1,10 @@
1
- #include "fmte.h"
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <altprintf/enums.h>
4
+ #include <altprintf/log.h>
5
+ #include <altprintf/fmte.h>
2
6
 
3
- struct fmte *fmte_ini()
7
+ struct fmte *fmte_ini(void)
4
8
  {
5
9
  struct fmte *f = malloc(sizeof(struct fmte));
6
10
 
@@ -12,10 +16,10 @@ struct fmte *fmte_ini()
12
16
  f->anglearg_end = NULL;
13
17
  f->anglearg_len = 0;
14
18
 
15
- f->chararg = L' ';
16
- f->padchar = L' ';
19
+ f->chararg = ' ';
20
+ f->padchar = ' ';
17
21
  f->type = FNone;
18
- f->align = Right;
22
+ f->align = Left;
19
23
  f->prec = -1;
20
24
  f->pad = 0;
21
25
  f->value = NULL;
@@ -49,8 +53,8 @@ void fmte_destroy(struct fmte *f)
49
53
 
50
54
  void fmte_inspect(struct fmte *f)
51
55
  {
52
- wchar_t *parenarg = calloc(f->parenarg_len + 1, sizeof(wchar_t));
53
- wchar_t *anglearg = calloc(f->anglearg_len + 1, sizeof(wchar_t));
56
+ char *parenarg = calloc(f->parenarg_len + 1, sizeof(char));
57
+ char *anglearg = calloc(f->anglearg_len + 1, sizeof(char));
54
58
  size_t i;
55
59
 
56
60
  for (i = 0; i < f->parenarg_len; i++) parenarg[i] = f->parenarg_start[i];
@@ -61,11 +65,11 @@ void fmte_inspect(struct fmte *f)
61
65
  parenarg_start: %p,\n\
62
66
  parenarg_end: %p,\n\
63
67
  parenarg_len: %ld,\n\
64
- (parenarg): %ls,\n\
68
+ (parenarg): %s,\n\
65
69
  anglearg_start: %p,\n\
66
70
  anglearg_end: %p,\n\
67
71
  anglearg_len: %ld,\n\
68
- (anglearg): %ls,\n\
72
+ (anglearg): %s,\n\
69
73
  chararg: %lc,\n\
70
74
  padchar: %lc,\n\
71
75
  type: %d,\n\
@@ -1,15 +1,33 @@
1
- #include "parsef.h"
1
+ #include <altprintf/fmte.h>
2
+ #include <altprintf/syntax.h>
3
+ #include <altprintf/log.h>
4
+ #include <altprintf/parsef.h>
2
5
 
3
- wchar_t *altprintf_pct = L"%";
6
+ char *altprintf_pct = "%";
4
7
 
5
- struct fmte *parsef(wchar_t **fmt)
8
+ void get_longarg(const char **s, const char **e, char stop, size_t *size)
6
9
  {
7
- wchar_t *w_c;
8
- wchar_t **w_a = &w_c;
10
+ *size = 0;
11
+
12
+ while (**s != EOS && **s != stop) {
13
+ LOG("checking (%c)\n", **s);
14
+ (*s)++;
15
+ (*size)++;
16
+ }
17
+
18
+ (*size)--;
19
+ if (*size > 0)
20
+ *e = *s - 1;
21
+ }
22
+
23
+ struct fmte *parsef(const char **fmt)
24
+ {
25
+ char *w_c;
26
+ char **w_a = &w_c;
9
27
  struct fmte *f = fmte_ini();
10
28
  long *l_a = &f->pad;
11
29
 
12
- LOG("processing %ld (%lc)\n", **fmt, (wint_t)**fmt);
30
+ LOG("processing %d (%c)\n", **fmt, **fmt);
13
31
 
14
32
  if (**fmt != FS_START) {
15
33
  if (**fmt == EOS) {
@@ -34,8 +52,8 @@ struct fmte *parsef(wchar_t **fmt)
34
52
  (*fmt)++;
35
53
  }
36
54
 
37
- for (; **fmt != L'\0'; (*fmt)++) {
38
- LOG("scanned char '%lc'\n", (wint_t)(**fmt));
55
+ for (; **fmt != '\0'; (*fmt)++) {
56
+ LOG("scanned char '%c'\n", **fmt);
39
57
  switch (**fmt) {
40
58
  case FS_A_CHARARG:
41
59
  (*fmt)++;
@@ -55,19 +73,18 @@ struct fmte *parsef(wchar_t **fmt)
55
73
  case FS_A_CALIGN:
56
74
  f->align = Center;
57
75
  break;
58
- case FS_A_SPAD:
59
- f->padchar = FS_A_SPAD;
60
- break;
61
- case FS_A_ZPAD:
62
- f->padchar = '0';
76
+ case FS_A_PAD:
77
+ (*fmt)++;
78
+ f->padchar = **fmt;
63
79
  break;
64
80
  case FS_A_PREC:
65
81
  l_a = &f->prec;
66
82
  break;
67
83
  case '1': case '2': case '3': case '4': case '5':
68
- case '6': case '7': case '8': case '9':
84
+ case '6': case '7': case '8': case '9': case '0':
69
85
  LOG("l_a: %p %ld\n", l_a, *l_a);
70
- *l_a = wcstol(*fmt, w_a, 10);
86
+ *l_a = strtol(*fmt, w_a, 10);
87
+ LOG("read num: %ld\n", *l_a);
71
88
  *fmt = *w_a - 1;
72
89
  break;
73
90
  // Psuedo-type
@@ -110,18 +127,3 @@ return_format:
110
127
  (*fmt)++;
111
128
  return f;
112
129
  }
113
-
114
- void get_longarg(wchar_t **s, wchar_t **e, wchar_t stop, size_t *size)
115
- {
116
- *size = 0;
117
-
118
- while (**s != EOS && **s != stop) {
119
- LOG("checking (%lc)\n", (wint_t)**s);
120
- (*s)++;
121
- (*size)++;
122
- }
123
-
124
- (*size)--;
125
- if (*size > 0)
126
- *e = *s - 1;
127
- }
@@ -1,8 +1,18 @@
1
- #include "strbuf.h"
1
+ #define STRBUF_INI_SIZE 5
2
+ #define STRBUF_GROW_STEP 100
3
+ #define TMPLEN 50
4
+ #define MAXPREC 25
5
+
6
+ #include <stdlib.h>
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+ #include <altprintf/log.h>
10
+ #include <altprintf/strbuf.h>
11
+ #include <altprintf/cwidth.h>
2
12
 
3
13
  extern struct lconv *locale_info;
4
14
 
5
- struct strbuf *strbuf_new()
15
+ struct strbuf *strbuf_new(void)
6
16
  {
7
17
  struct strbuf *sb = malloc(sizeof(struct strbuf));
8
18
 
@@ -11,7 +21,7 @@ struct strbuf *strbuf_new()
11
21
  exit(1);
12
22
  }
13
23
 
14
- sb->start = sb->end = calloc(STRBUF_INI_SIZE, sizeof(wchar_t));
24
+ sb->start = sb->end = calloc(STRBUF_INI_SIZE, sizeof(char));
15
25
 
16
26
  if (NULL == sb->start) {
17
27
  LOG("can't alloc memory for new strbuf string\n");
@@ -27,7 +37,7 @@ struct strbuf *strbuf_new()
27
37
  size_t strbuf_width(struct strbuf *sb)
28
38
  {
29
39
  if (sb->width == 0)
30
- sb->width = wcswidth(sb->start, sb->len);
40
+ sb->width = cswidth(sb->start, sb->len);
31
41
 
32
42
  return sb->width;
33
43
  }
@@ -38,36 +48,33 @@ void strbuf_destroy(struct strbuf *sb)
38
48
  free(sb);
39
49
  }
40
50
 
41
- void strbuf_append(struct strbuf *sb, wchar_t c)
51
+ void strbuf_append(struct strbuf *sb, char c)
42
52
  {
43
- wchar_t *ns;
53
+ char *ns;
44
54
 
45
55
  if (sb->cap < sb->len + 2) {
46
- ns = calloc(sb->cap + STRBUF_GROW_STEP, sizeof(wchar_t));
56
+ ns = realloc(sb->start, (sb->cap + STRBUF_GROW_STEP) * sizeof(char));
47
57
 
48
58
  if (ns == NULL) {
49
59
  LOG("can't increase size of strbuf to %d\n", sb->cap + STRBUF_GROW_STEP);
50
60
  exit(1);
51
61
  }
52
62
 
53
- wcscpy(ns, sb->start);
54
- free(sb->start);
55
-
56
63
  sb->start = ns;
57
64
  sb->cap += STRBUF_GROW_STEP;
58
65
  }
59
66
 
60
67
  sb->start[sb->len] = c;
61
- sb->start[sb->len + 1] = L'\0';
62
- LOG("string so far: %ls\n", sb->start);
68
+ sb->start[sb->len + 1] = '\0';
69
+ LOG("string so far: %s\n", sb->start);
63
70
  sb->end = &sb->start[sb->len];
64
71
  sb->len++;
65
72
  }
66
73
 
67
- void strbuf_append_strbuf(struct strbuf *sb, void *sbuf)
74
+ void strbuf_append_strbuf(struct strbuf *sb, const void *sbuf)
68
75
  {
69
- wchar_t *pos;
70
- struct strbuf *frm = sbuf;
76
+ char *pos;
77
+ const struct strbuf *frm = sbuf;
71
78
 
72
79
  LOG("frm->start: %p | frm->end: %p\n", frm->start, frm->end);
73
80
 
@@ -75,16 +82,16 @@ void strbuf_append_strbuf(struct strbuf *sb, void *sbuf)
75
82
  strbuf_append(sb, *pos);
76
83
  }
77
84
 
78
- void strbuf_appendw_strbuf(struct strbuf *sb, void *sbuf, long w)
85
+ void strbuf_appendw_strbuf(struct strbuf *sb, const void *sbuf, long w)
79
86
  {
80
- wchar_t *pos;
87
+ char *pos;
81
88
  long ws = 0;
82
- struct strbuf *frm = sbuf;
89
+ const struct strbuf *frm = sbuf;
83
90
 
84
91
  LOG("frm->start: %p | frm->end: %p\n", frm->start, frm->end);
85
92
 
86
93
  for (pos = frm->start; pos <= frm->end; pos++) {
87
- ws += wcwidth(*pos);
94
+ ws += cwidth(pos);
88
95
  LOG("new width would be: %ld, requested width: %ld\n", ws, w);
89
96
  if (ws > w)
90
97
  break;
@@ -92,17 +99,17 @@ void strbuf_appendw_strbuf(struct strbuf *sb, void *sbuf, long w)
92
99
  }
93
100
  }
94
101
 
95
- void strbuf_append_char(struct strbuf *sb, void *chr)
102
+ void strbuf_append_char(struct strbuf *sb, const void *chr)
96
103
  {
97
- wint_t *c = chr;
104
+ const char *c = chr;
98
105
 
99
106
  strbuf_append(sb, *c);
100
107
  }
101
108
 
102
- void strbuf_append_str(struct strbuf *sb, void *str, int maxwidth)
109
+ void strbuf_append_str(struct strbuf *sb, const void *str, int maxwidth)
103
110
  {
104
- wchar_t *s = str;
105
- wchar_t *end = &s[wcslen(s)];
111
+ const char *s = str;
112
+ const char *end = &s[strlen(s)];
106
113
  int width = 0;
107
114
  int maxlen = -1;
108
115
 
@@ -115,7 +122,7 @@ void strbuf_append_str(struct strbuf *sb, void *str, int maxwidth)
115
122
  if (width > maxlen)
116
123
  return;
117
124
  } else {
118
- width += wcwidth(*s);
125
+ width += cwidth(s);
119
126
  if (width > maxwidth)
120
127
  return;
121
128
  }
@@ -124,55 +131,56 @@ void strbuf_append_str(struct strbuf *sb, void *str, int maxwidth)
124
131
  }
125
132
  }
126
133
 
127
- void strbuf_append_int(struct strbuf *sb, void *in)
134
+ void strbuf_append_int(struct strbuf *sb, const void *in)
128
135
  {
129
- long int *i = in;
130
- wchar_t wcs[TMPLEN];
131
- long len = swprintf(wcs, TMPLEN - 1, L"%ld", *i);
136
+ const long *i = in;
137
+ char wcs[TMPLEN];
138
+ long len = snprintf(wcs, TMPLEN - 1, "%ld", *i);
132
139
 
133
140
  if (len < 0)
134
- strbuf_append_str(sb, L"error adding int", 16);
141
+ strbuf_append_str(sb, "error adding int", 16);
135
142
  else
136
143
  strbuf_append_str(sb, wcs, -1 * len);
137
144
  }
138
145
 
139
- void strbuf_append_double(struct strbuf *sb, void *dub, int prec)
146
+ void strbuf_append_double(struct strbuf *sb, const void *dub, int prec)
140
147
  {
141
- double *d = dub;
142
- wchar_t wcs[TMPLEN];
143
- wchar_t format[TMPLEN];
148
+ const double *d = dub;
149
+ char wcs[TMPLEN];
150
+ char format[TMPLEN];
144
151
  int rprec = prec;
145
152
 
146
153
  if (rprec > MAXPREC)
147
154
  rprec = MAXPREC;
148
155
 
149
- swprintf(format, TMPLEN - 1, L"%%.%ldf", rprec);
156
+ snprintf(format, TMPLEN - 1, "%%.%df", rprec);
150
157
 
151
- LOG("format: %ls\n", format);
158
+ LOG("format: %s\n", format);
152
159
 
153
- long len = swprintf(wcs, TMPLEN - 1, format, *d);
160
+ long len = snprintf(wcs, TMPLEN - 1, format, *d);
154
161
  if (len < 0) {
155
- strbuf_append_str(sb, L"error adding float", 18);
162
+ strbuf_append_str(sb, "error adding float", 18);
156
163
  } else {
157
164
  LOG("inserting double: len: %ld\n", len);
158
165
  strbuf_append_str(sb, wcs, len);
159
166
 
160
167
  if (rprec < prec)
161
- strbuf_pad(sb, L'0', rprec - prec);
168
+ strbuf_pad(sb, '0', rprec - prec);
162
169
  }
163
170
  }
164
171
 
165
- void strbuf_pad(struct strbuf *sb, wchar_t pc, int amnt)
172
+ void strbuf_pad(struct strbuf *sb, char pc, int amnt)
166
173
  {
167
174
  for (; amnt > 0; amnt--) strbuf_append(sb, pc);
168
175
  }
169
176
 
170
- wchar_t *strbuf_cstr(struct strbuf *sb)
177
+ char *strbuf_cstr(struct strbuf *sb)
171
178
  {
172
- wchar_t *str;
179
+ char *cstr;
180
+
181
+ cstr = calloc(sb->len + 1, sizeof(char));
173
182
 
174
- str = calloc(sb->len + 1, sizeof(wchar_t));
175
- wcscpy(str, sb->start);
183
+ strncpy(cstr, sb->start, sb->len);
176
184
 
177
- return str;
185
+ return cstr;
178
186
  }
Binary file
@@ -1,3 +1,3 @@
1
1
  module Altprintf
2
- VERSION = [0, 2, 4]
2
+ VERSION = [0, 3, 0]
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: altprintf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
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-27 00:00:00.000000000 Z
11
+ date: 2019-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: benchmark-ips
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake-compiler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '12.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
41
69
  description:
42
70
  email: lattis@mochiro.moe
43
71
  executables: []
@@ -45,21 +73,22 @@ extensions: []
45
73
  extra_rdoc_files: []
46
74
  files:
47
75
  - altprintf.gemspec
48
- - ext/altprintf/altprintf.h
49
- - ext/altprintf/enums.h
76
+ - ext/altprintf/altprintf/altprintf.h
77
+ - ext/altprintf/altprintf/cwidth.h
78
+ - ext/altprintf/altprintf/enums.h
79
+ - ext/altprintf/altprintf/fmt.h
80
+ - ext/altprintf/altprintf/fmte.h
81
+ - ext/altprintf/altprintf/log.h
82
+ - ext/altprintf/altprintf/parsef.h
83
+ - ext/altprintf/altprintf/strbuf.h
84
+ - ext/altprintf/altprintf/syntax.h
50
85
  - ext/altprintf/ext.c
51
86
  - ext/altprintf/extconf.h
52
87
  - ext/altprintf/extconf_helper.rb
53
88
  - ext/altprintf/fmt.c
54
- - ext/altprintf/fmt.h
55
89
  - ext/altprintf/fmte.c
56
- - ext/altprintf/fmte.h
57
- - ext/altprintf/log.h
58
90
  - ext/altprintf/parsef.c
59
- - ext/altprintf/parsef.h
60
91
  - ext/altprintf/strbuf.c
61
- - ext/altprintf/strbuf.h
62
- - ext/altprintf/syntax.h
63
92
  - lib/altprintf.rb
64
93
  - lib/altprintf/alt_printf.so
65
94
  - lib/altprintf/altprintf.so
@@ -77,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
106
  requirements:
78
107
  - - ">="
79
108
  - !ruby/object:Gem::Version
80
- version: 2.5.5
109
+ version: 2.5.7
81
110
  required_rubygems_version: !ruby/object:Gem::Requirement
82
111
  requirements:
83
112
  - - ">="
@@ -1,6 +0,0 @@
1
- #ifndef ALTPRINTF_H_
2
- #define ALTPRINTF_H_
3
- #define ALTPRINTF_VERSION "0.2.4"
4
- #include "parsef.h"
5
- #include "fmt.h"
6
- #endif
data/ext/altprintf/fmt.h DELETED
@@ -1,9 +0,0 @@
1
- #ifndef FMT_H_
2
- #define FMT_H_
3
- #include "syntax.h"
4
- #include "strbuf.h"
5
- #include "fmte.h"
6
- #include "log.h"
7
-
8
- wchar_t *assemble_fmt(struct fmte *);
9
- #endif
@@ -1,10 +0,0 @@
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,38 +0,0 @@
1
- #ifndef STRBUF_H
2
- #define STRBUF_H
3
-
4
- #ifndef _XOPEN_SOURCE
5
- #define _XOPEN_SOURCE
6
- #endif
7
-
8
- #include <wchar.h>
9
- #include <stdlib.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
17
-
18
- struct strbuf *strbuf_new();
19
- void strbuf_destroy(struct strbuf *sb);
20
- void strbuf_append(struct strbuf *sb, wchar_t);
21
- void strbuf_append_char(struct strbuf *sb, void *);
22
- void strbuf_append_str(struct strbuf *, void *, int);
23
- void strbuf_append_int(struct strbuf *sb, void *);
24
- void strbuf_append_double(struct strbuf *, void *, int);
25
- void strbuf_append_strbuf(struct strbuf *, void *);
26
- void strbuf_appendw_strbuf(struct strbuf *, void *, long);
27
- void strbuf_pad(struct strbuf *, wchar_t, int);
28
- wchar_t *strbuf_cstr(struct strbuf *);
29
- size_t strbuf_width(struct strbuf *);
30
-
31
- struct strbuf {
32
- wchar_t *start;
33
- wchar_t *end;
34
- size_t len;
35
- size_t width;
36
- size_t cap;
37
- };
38
- #endif