diff -ur v19/ezyaml.c v20/ezyaml.c --- v19/ezyaml.c 2018-09-12 22:21:15.000000000 +0900 +++ v20/ezyaml.c 2018-09-12 22:21:45.000000000 +0900 @@ -29,11 +29,20 @@ r = cp_add( r, strdup(to) ); s = t + strlen(from); } - r = cp_add(r, s); + r = cp_add( r, strdup(s) ); free(bak); /* ! */ return r; } +char * +cp_char_n(char v, int n) +{ + char *r = malloc(n+1); + memset(r, v, n); + r[n] = '\0'; + return r; +} + /**/ void @@ -395,7 +404,7 @@ struct obj *str = list_pop_top(lines); char *s = slice_tail( obj_buf(str), idt ); struct obj *add = get_cont_str(idt, lines); - struct obj *ret = str_new(" "); + struct obj *ret = str_new(" "); ret = str_append(ret, s); ret = str_append( ret, obj_buf(add) ); @@ -492,6 +501,54 @@ struct obj *dump_obj(struct obj *obj, int idt); struct obj * +wrap_line(struct obj *str, int idt) +{ + struct obj *lst; + char *sum; + char *s = obj_buf(str); + + if( strchr(s, ' ') == NULL ){ + return str; + } + + s = cp_replace( strdup(s), " ", "\n" ); + lst = lines_new(s); + free(s); + obj_del(str); + + sum = strdup(""); + + str = list_pop_top(lst); + s = strdup( obj_buf(str) ); + obj_del(str); + + while( list_len(lst) ){ + char *t; + while( list_len(lst) && strlen(s) + 2 <= 82 ){ + str = list_pop_top(lst); + t = strdup( obj_buf(str) ); + obj_del(str); + s = cp_add( s, strdup(" ") ); + s = cp_add(s, t); + } + if( list_len(lst) ){ + sum = cp_add(sum, s); + sum = cp_add( sum, strdup("\n") ); + s = cp_char_n(' ', idt+2); + + str = list_pop_top(lst); + t = strdup( obj_buf(str) ); + obj_del(str); + s = cp_add(s, t); + } + } + sum = cp_add(sum, s); + str = str_new(sum); + free(sum); + return str; +} + +struct obj * dump_dict(struct obj *dic, int idt) { struct obj *lines = list_new(); @@ -515,6 +572,7 @@ tmp = dump_value(v, 'r'); str = str_append( str, obj_buf(tmp) ); obj_del(tmp); + str = wrap_line(str, idt); } list_append(lines, str); @@ -643,13 +701,12 @@ dump_other(struct obj *obj, int idt, int from_dic) { struct obj *str = dump_value(obj, from_dic); - char *s = malloc(idt+1); - memset(s, ' ', idt); - s[idt] = '\0'; - + char *s = cp_char_n(' ', idt); s = cp_add( s, strdup( obj_buf(str) ) ); obj_del(str); - return str_new(s); + str = wrap_line( str_new(s), idt); + free(s); + return str; } struct obj * diff -ur v19/ezyaml.py v20/ezyaml.py --- v19/ezyaml.py 2018-09-12 22:21:15.000000000 +0900 +++ v20/ezyaml.py 2018-09-12 22:21:45.000000000 +0900 @@ -200,6 +200,21 @@ ### +def wrap_line(s, idt): + if ' ' not in s: + return s + lst = s.split(' ') + sum = '' + s = lst.pop(0) + while lst: + while lst and len(s) + 2 <= 82: + s += ' ' + lst.pop(0) + if lst: + sum += s + '\n' + s = ' ' * (idt+2) + lst.pop(0) + sum += s + return sum + def dump_dict(dic, idt): lines = [] for (k, v) in dic.items(): @@ -212,6 +227,7 @@ s = dump_dict(v, idt+2) else: s += ' ' + dump_value(v, from_dic='r') + s = wrap_line(s, idt) lines.append(s) return '\n'.join(lines) @@ -267,8 +283,9 @@ return '{}'.format(obj) def dump_other(obj, idt, from_dic=None): - return (' ' * idt) + dump_value(obj, from_dic) - + s = (' ' * idt) + dump_value(obj, from_dic) + return wrap_line(s, idt) + def dump_obj(obj, idt): t = type(obj) if t == dict and obj: