• R/O
  • SSH
  • HTTPS

yash: 提交


Commit MetaInfo

修訂4076 (tree)
時間2020-09-20 16:19:41
作者magicant

Log Message

sb_initwithmax and wb_initwithmax

Change Summary

差異

--- yash/trunk/builtin.c (revision 4075)
+++ yash/trunk/builtin.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* builtin.c: built-in commands */
3-/* (C) 2007-2018 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -422,7 +422,7 @@
422422 bool print_short_option = there_is_any_short_option(options);
423423
424424 xstrbuf_T line;
425- sb_init(&line);
425+ sb_initwithmax(&line, 127);
426426
427427 for (const struct xgetopt_T *opt = options; opt->shortopt != L'\0'; opt++) {
428428 if (posixly_correct && !opt->posix)
--- yash/trunk/expand.c (revision 4075)
+++ yash/trunk/expand.c (revision 4076)
@@ -845,7 +845,7 @@
845845 pl_add(&e->e.cclist, sb_tostr(&e->ccbuf));
846846
847847 wb_initwith(&e->valuebuf, values[i]);
848- sb_init(&e->ccbuf);
848+ sb_initwithmax(&e->ccbuf, e->valuebuf.length);
849849 fill_ccbuf(e, cc);
850850 }
851851 }
@@ -1165,11 +1165,12 @@
11651165 size_t lastelemindex = splitpoints.length - 1;
11661166 size_t headlen = idx(splitpoints.contents[0]) - 1;
11671167 size_t taillen = wcslen(splitpoints.contents[lastelemindex]);
1168+ size_t totallen = idx(splitpoints.contents[lastelemindex]) + taillen;
11681169 for (size_t i = 0; i < lastelemindex; i++) {
11691170 xwcsbuf_T buf;
11701171 xstrbuf_T cbuf;
1171- wb_init(&buf);
1172- sb_init(&cbuf);
1172+ wb_initwithmax(&buf, totallen);
1173+ sb_initwithmax(&cbuf, totallen);
11731174
11741175 wb_ncat_force(&buf, word, headlen);
11751176 sb_ncat_force(&cbuf, cc, headlen);
@@ -1268,8 +1269,8 @@
12681269 do {
12691270 xwcsbuf_T buf;
12701271 xstrbuf_T cbuf;
1271- wb_init(&buf);
1272- sb_init(&cbuf);
1272+ wb_initwithmax(&buf, wordlen);
1273+ sb_initwithmax(&cbuf, wordlen);
12731274
12741275 size_t slen = idx(startc - 1);
12751276 wb_ncat_force(&buf, word, slen);
@@ -1510,7 +1511,7 @@
15101511 wchar_t *escape(const wchar_t *restrict s, const wchar_t *restrict t)
15111512 {
15121513 xwcsbuf_T buf;
1513- wb_init(&buf);
1514+ wb_initwithmax(&buf, mul(wcslen(s), 2));
15141515 for (size_t i = 0; s[i] != L'\0'; i++) {
15151516 if (t == NULL || wcschr(t, s[i]) != NULL)
15161517 wb_wccat(&buf, L'\\');
@@ -1536,7 +1537,7 @@
15361537 wchar_t *unescape(const wchar_t *s)
15371538 {
15381539 xwcsbuf_T buf;
1539- wb_init(&buf);
1540+ wb_initwithmax(&buf, wcslen(s));
15401541 for (size_t i = 0; s[i] != L'\0'; i++) {
15411542 if (s[i] == L'\\') {
15421543 i++;
@@ -1565,7 +1566,7 @@
15651566 wchar_t *quote_as_word(const wchar_t *s)
15661567 {
15671568 xwcsbuf_T buf;
1568- wb_init(&buf);
1569+ wb_initwithmax(&buf, mul(wcslen(s), 2));
15691570 wb_quote_as_word(&buf, s);
15701571 return wb_towcs(&buf);
15711572 }
@@ -1619,7 +1620,7 @@
16191620 {
16201621 bool indq = false;
16211622 xwcsbuf_T buf;
1622- wb_init(&buf);
1623+ wb_initwithmax(&buf, wcslen(s));
16231624 for (;;) {
16241625 switch (*s) {
16251626 case L'\0':
@@ -1670,7 +1671,7 @@
16701671 const wchar_t *restrict s, const char *restrict cc, escaping_T escaping)
16711672 {
16721673 xwcsbuf_T result;
1673- wb_init(&result);
1674+ wb_initwithmax(&result, mul(wcslen(s), 2));
16741675 for (size_t i = 0; s[i] != L'\0'; i++) {
16751676 if (cc[i] & CC_QUOTATION)
16761677 continue;
--- yash/trunk/history.c (revision 4075)
+++ yash/trunk/history.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* history.c: command history management */
3-/* (C) 2007-2016 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -77,7 +77,11 @@
7777 #error DEFAULT_HISTSIZE cannot be larger than HISTORY_MIN_MAX_NUMBER
7878 #endif
7979
80+#ifndef HISTORY_DEFAULT_LINE_LENGTH
81+#define HISTORY_DEFAULT_LINE_LENGTH 127
82+#endif
8083
84+
8185 /* The main history list. */
8286 histlist_T histlist = {
8387 .link = { Histlist, Histlist, },
@@ -649,7 +653,7 @@
649653
650654 assert(histfile != NULL);
651655 rewind(histfile);
652- if (!read_line(histfile, wb_init(&buf)))
656+ if (!read_line(histfile, wb_initwithmax(&buf, HISTORY_DEFAULT_LINE_LENGTH)))
653657 goto end;
654658
655659 s = matchwcsprefix(buf.contents, L"#$# yash history v0 r");
@@ -675,7 +679,7 @@
675679 xwcsbuf_T buf;
676680
677681 assert(histfile != NULL);
678- wb_init(&buf);
682+ wb_initwithmax(&buf, HISTORY_DEFAULT_LINE_LENGTH);
679683 while (read_line(histfile, &buf)) {
680684 char *line = malloc_wcstombs(buf.contents);
681685 if (line != NULL) {
@@ -699,7 +703,7 @@
699703 xwcsbuf_T buf;
700704
701705 assert(histfile != NULL);
702- wb_init(&buf);
706+ wb_initwithmax(&buf, HISTORY_DEFAULT_LINE_LENGTH);
703707 while (read_line(histfile, &buf)) {
704708 histfilelines++;
705709 switch (buf.contents[0]) {
@@ -1606,7 +1610,7 @@
16061610 update_time();
16071611 update_history(false);
16081612
1609- wb_init(&buf);
1613+ wb_initwithmax(&buf, HISTORY_DEFAULT_LINE_LENGTH);
16101614 while (read_line(f, &buf)) {
16111615 if (!quiet)
16121616 printf("%ls\n", buf.contents);
--- yash/trunk/lineedit/compparse.c (revision 4075)
+++ yash/trunk/lineedit/compparse.c (revision 4076)
@@ -117,7 +117,7 @@
117117 le_context_T *ctxt = xmalloc(sizeof *ctxt);
118118
119119 cparseinfo_T parseinfo;
120- wb_init(&parseinfo.buf);
120+ wb_initwithmax(&parseinfo.buf, le_main_index);
121121 wb_ncat_force(&parseinfo.buf, le_main_buffer.contents, le_main_index);
122122 parseinfo.bufindex = 0;
123123 parseinfo.aliaslist = NULL;
@@ -1207,7 +1207,7 @@
12071207 bool unclosed = false;
12081208 xwcsbuf_T buf;
12091209
1210- wb_init(&buf);
1210+ wb_initwithmax(&buf, wcslen(s));
12111211 wb_cat(&buf, s);
12121212
12131213 for (size_t i = 0; i < buf.length; ) {
--- yash/trunk/lineedit/editing.c (revision 4075)
+++ yash/trunk/lineedit/editing.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* editing.c: main editing module */
3-/* (C) 2007-2017 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -3143,7 +3143,7 @@
31433143 }
31443144 if (w1end >= w2start)
31453145 goto error;
3146- wb_init(&buf);
3146+ wb_initwithmax(&buf, w2end - w1start);
31473147 wb_ncat_force(&buf, &le_main_buffer.contents[w2start], w2end - w2start);
31483148 wb_ncat_force(&buf, &le_main_buffer.contents[w1end], w2start - w1end);
31493149 wb_ncat_force(&buf, &le_main_buffer.contents[w1start], w1end - w1start);
--- yash/trunk/option.c (revision 4075)
+++ yash/trunk/option.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* option.c: option settings */
3-/* (C) 2007-2018 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -398,7 +398,7 @@
398398 wchar_t *normalize_option_name(const wchar_t *optname)
399399 {
400400 xwcsbuf_T result;
401- wb_init(&result);
401+ wb_initwithmax(&result, wcslen(optname));
402402 for (const wchar_t *s = optname; *s != L'\0'; s++)
403403 if (iswalnum(*s))
404404 wb_wccat(&result, towlower(*s));
--- yash/trunk/path.c (revision 4075)
+++ yash/trunk/path.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* path.c: filename-related utilities */
3-/* (C) 2007-2016 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -323,7 +323,7 @@
323323 n = (uintmax_t) shell_pid * 272229637312669;
324324 if (num == 0)
325325 num = (uintmax_t) time(NULL) * 5131212142718371 << 1 | 1;
326- sb_init(&buf);
326+ sb_initwithmax(&buf, 31);
327327 for (int i = 0; i < 100; i++) {
328328 num = (num ^ n) * 16777619;
329329 sb_printf(&buf, "/tmp/yash-%" PRIXMAX, num);
@@ -641,18 +641,18 @@
641641 bool wglob(const wchar_t *restrict pattern, enum wglobflags_T flags,
642642 plist_T *restrict list)
643643 {
644- size_t listbase = list->length;
644+ size_t listbase = list->length, patternsize = add(wcslen(pattern), 1);
645645 xstrbuf_T path;
646646 xwcsbuf_T wpath;
647647 struct wglob_pattern *p;
648- wchar_t savepattern[wcslen(pattern) + 1];
648+ wchar_t savepattern[patternsize];
649649
650650 p = wglob_parse_pattern(wcscpy(savepattern, pattern), flags);
651651 if (p == NULL)
652652 return false;
653653
654- sb_init(&path);
655- wb_init(&wpath);
654+ sb_initwithmax(&path, patternsize);
655+ wb_initwithmax(&wpath, patternsize);
656656 wglob_search(p, flags, &path, &wpath, list);
657657 sb_destroy(&path);
658658 wb_destroy(&wpath);
@@ -1595,7 +1595,7 @@
15951595 {
15961596 xstrbuf_T outputtext;
15971597
1598- sb_init(&outputtext);
1598+ sb_initwithmax(&outputtext, 18);
15991599 sb_ccat(&outputtext, 'u');
16001600 sb_ccat(&outputtext, '=');
16011601 if (!(mode & S_IRUSR)) sb_ccat(&outputtext, 'r');
--- yash/trunk/strbuf.c (revision 4075)
+++ yash/trunk/strbuf.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* strbuf.c: modifiable string buffer */
3-/* (C) 2007-2015 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -36,14 +36,7 @@
3636 size_t len, mbstate_t *restrict ps);
3737 #endif
3838
39-#ifndef XSTRBUF_INITSIZE
40-#define XSTRBUF_INITSIZE 15
41-#endif
42-#ifndef XWCSBUF_INITSIZE
43-#define XWCSBUF_INITSIZE 15
44-#endif
4539
46-
4740 /* If the type of the return value of the functions below is string buffer,
4841 * the return value is the argument buffer. */
4942
@@ -51,13 +44,13 @@
5144 /********** Multibyte String Buffer **********/
5245
5346 /* Initializes the specified string buffer as an empty string. */
54-xstrbuf_T *sb_init(xstrbuf_T *buf)
47+xstrbuf_T *sb_initwithmax(xstrbuf_T *buf, size_t max)
5548 {
56- // buf->contents = xmallocn(XSTRBUF_INITSIZE + 1, sizeof (char));
57- buf->contents = xmalloc(XSTRBUF_INITSIZE + 1);
49+ // buf->contents = xmalloce(max, 1, sizeof (char));
50+ buf->contents = xmalloc(add(max, 1));
5851 buf->contents[0] = '\0';
5952 buf->length = 0;
60- buf->maxlength = XSTRBUF_INITSIZE;
53+ buf->maxlength = max;
6154 return buf;
6255 }
6356
@@ -322,12 +315,12 @@
322315 /********** Wide String Buffer **********/
323316
324317 /* Initializes the specified wide string buffer as an empty string. */
325-xwcsbuf_T *wb_init(xwcsbuf_T *buf)
318+xwcsbuf_T *wb_initwithmax(xwcsbuf_T *buf, size_t max)
326319 {
327- buf->contents = xmallocn(XWCSBUF_INITSIZE + 1, sizeof (wchar_t));
320+ buf->contents = xmalloce(max, 1, sizeof (wchar_t));
328321 buf->contents[0] = L'\0';
329322 buf->length = 0;
330- buf->maxlength = XWCSBUF_INITSIZE;
323+ buf->maxlength = max;
331324 return buf;
332325 }
333326
--- yash/trunk/strbuf.h (revision 4075)
+++ yash/trunk/strbuf.h (revision 4076)
@@ -25,7 +25,14 @@
2525
2626 #define Size_max ((size_t) -1) // = SIZE_MAX
2727
28+#ifndef XSTRBUF_INITSIZE
29+#define XSTRBUF_INITSIZE 15
30+#endif
31+#ifndef XWCSBUF_INITSIZE
32+#define XWCSBUF_INITSIZE 15
33+#endif
2834
35+
2936 typedef struct xstrbuf_T {
3037 char *contents;
3138 size_t length, maxlength;
@@ -35,10 +42,12 @@
3542 size_t length, maxlength;
3643 } xwcsbuf_T;
3744
38-extern xstrbuf_T *sb_init(xstrbuf_T *buf)
45+static inline xstrbuf_T *sb_init(xstrbuf_T *buf)
3946 __attribute__((nonnull));
4047 extern xstrbuf_T *sb_initwith(xstrbuf_T *restrict buf, char *restrict s)
4148 __attribute__((nonnull));
49+extern xstrbuf_T *sb_initwithmax(xstrbuf_T *buf, size_t max)
50+ __attribute__((nonnull));
4251 static inline void sb_destroy(xstrbuf_T *buf)
4352 __attribute__((nonnull));
4453 static inline char *sb_tostr(xstrbuf_T *buf)
@@ -107,10 +116,12 @@
107116 xstrbuf_T *restrict buf, const char *restrict format, ...)
108117 __attribute__((nonnull(1,2),format(printf,2,3)));
109118
110-extern xwcsbuf_T *wb_init(xwcsbuf_T *buf)
119+static inline xwcsbuf_T *wb_init(xwcsbuf_T *buf)
111120 __attribute__((nonnull));
112121 extern xwcsbuf_T *wb_initwith(xwcsbuf_T *restrict buf, wchar_t *restrict s)
113122 __attribute__((nonnull));
123+extern xwcsbuf_T *wb_initwithmax(xwcsbuf_T *buf, size_t max)
124+ __attribute__((nonnull));
114125 static inline void wb_destroy(xwcsbuf_T *buf)
115126 __attribute__((nonnull));
116127 static inline wchar_t *wb_towcs(xwcsbuf_T *buf)
@@ -194,6 +205,12 @@
194205 __attribute__((malloc,warn_unused_result,nonnull));
195206
196207
208+/* Initializes the specified string buffer as an empty string. */
209+xstrbuf_T *sb_init(xstrbuf_T *buf)
210+{
211+ return sb_initwithmax(buf, XSTRBUF_INITSIZE);
212+}
213+
197214 /* Frees the specified multibyte string buffer. The contents are lost. */
198215 void sb_destroy(xstrbuf_T *buf)
199216 {
@@ -310,6 +327,12 @@
310327 }
311328 #endif
312329
330+/* Initializes the specified wide string buffer as an empty string. */
331+xwcsbuf_T *wb_init(xwcsbuf_T *buf)
332+{
333+ return wb_initwithmax(buf, XWCSBUF_INITSIZE);
334+}
335+
313336 /* Frees the specified wide string buffer. The contents are lost. */
314337 void wb_destroy(xwcsbuf_T *buf)
315338 {
--- yash/trunk/variable.c (revision 4075)
+++ yash/trunk/variable.c (revision 4076)
@@ -1852,7 +1852,7 @@
18521852 case L't':
18531853 assert(wcscmp(argv0, L"typeset") == 0);
18541854 typeset:
1855- sb_init(&opts);
1855+ sb_initwithmax(&opts, 4);
18561856 if (var->v_type & VF_EXPORT)
18571857 sb_ccat(&opts, 'x');
18581858 if (var->v_type & VF_READONLY)
@@ -1913,7 +1913,7 @@
19131913 case L't':
19141914 assert(wcscmp(argv0, L"typeset") == 0);
19151915 typeset:
1916- sb_init(&opts);
1916+ sb_initwithmax(&opts, 4);
19171917 if (var->v_type & VF_EXPORT)
19181918 sb_ccat(&opts, 'x');
19191919 if (var->v_type & VF_READONLY)
--- yash/trunk/yash.c (revision 4075)
+++ yash/trunk/yash.c (revision 4076)
@@ -1,6 +1,6 @@
11 /* Yash: yet another shell */
22 /* yash.c: basic functions of the shell */
3-/* (C) 2007-2018 magicant */
3+/* (C) 2007-2020 magicant */
44
55 /* This program is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License as published by
@@ -303,7 +303,7 @@
303303 return false;
304304
305305 xwcsbuf_T fullpath;
306- wb_init(&fullpath);
306+ wb_initwithmax(&fullpath, add(add(wcslen(home), wcslen(path)), 1));
307307 wb_cat(&fullpath, home);
308308 if (fullpath.contents[fullpath.length - 1] != L'/')
309309 wb_wccat(&fullpath, L'/');
Show on old repository browser