diff -ur v15/ezyaml.c v16/ezyaml.c --- v15/ezyaml.c 2018-09-09 05:08:20.000000000 +0900 +++ v16/ezyaml.c 2018-09-10 21:21:46.000000000 +0900 @@ -116,7 +116,7 @@ continue; } if(qt){ - if( is_qts(c) ){ + if(c == qt){ buf = parse_append(buf, c); buf = parse_append(buf, '\0'); list_append( lst, str_new( obj_buf(buf) ) ); @@ -475,8 +475,8 @@ /**/ struct obj *dump_list(struct obj *lst, int idt); -struct obj *dump_value(struct obj *obj, int dic_right); -struct obj *dump_other(struct obj *obj, int idt); +struct obj *dump_value(struct obj *obj, int from_dic); +struct obj *dump_other(struct obj *obj, int idt, int from_dic); struct obj *dump_obj(struct obj *obj, int idt); struct obj * @@ -490,7 +490,7 @@ for(i=0; inext; - str = str_append( dump_other(k, idt), ":" ); + str = str_append( dump_other(k, idt, 'l'), ":" ); if( v->typ == OBJ_TYP_LIST && list_len(v) ){ list_append(lines, str); @@ -500,7 +500,7 @@ str = dump_dict(v, idt+2); }else{ str = str_append(str, " "); - tmp = dump_value(v, 1); + tmp = dump_value(v, 'r'); str = str_append( str, obj_buf(tmp) ); obj_del(tmp); } @@ -568,17 +568,27 @@ return r; } +char * +sel_quote(int q2, int q1, int cl, char *s, struct obj *obj) +{ + if(q2 || q1 || cl || is_not_same_str(s, obj) || need_quote(s) ){ + return q1 ? "\"" : "'"; + } + return NULL; +} + struct obj * -dump_value(struct obj *obj, int dic_right) +dump_value(struct obj *obj, int from_dic) { if(obj->typ == OBJ_TYP_STR){ struct obj *str; + char *qt; char *s = strdup( obj_buf(obj) ); int q2 = strchr(s, '"') != NULL; int q1 = strchr(s, '\'') != NULL; int cl = strchr(s, ':') != NULL; - if(cl && dic_right){ + if(cl && from_dic == 'r'){ char *t = s; cl = 0; while( ( t = strchr(t, ':') ) ){ @@ -593,8 +603,16 @@ s = cp_replace(s, "'", "\\'"); q1 = 0; } - if(q2 || q1 || cl || is_not_same_str(s, obj) || need_quote(s) ){ - char *qt = q1 ? "\"" : "'"; + if(from_dic != 'l'){ + if(q1 && !sel_quote(q2, 0, cl, s, obj)){ + q1 = 0; + } + if(q2 && !sel_quote(0, q1, cl, s, obj)){ + q2 = 0; + } + } + qt = sel_quote(q2, q1, cl, s, obj); + if(qt){ s = cp_add( strdup(qt), s ); s = cp_add( s, strdup(qt) ); } @@ -634,9 +652,9 @@ } struct obj * -dump_other(struct obj *obj, int idt) +dump_other(struct obj *obj, int idt, int from_dic) { - struct obj *str = dump_value(obj, 0); + struct obj *str = dump_value(obj, from_dic); char *s = malloc(idt+1); memset(s, ' ', idt); s[idt] = '\0'; @@ -656,7 +674,7 @@ if( t == OBJ_TYP_LIST && list_len(obj) ){ return dump_list(obj, idt); } - return dump_other(obj, idt); + return dump_other(obj, idt, 0); } struct obj * diff -ur v15/ezyaml.py v16/ezyaml.py --- v15/ezyaml.py 2018-09-10 21:16:31.000000000 +0900 +++ v16/ezyaml.py 2018-09-10 21:21:43.000000000 +0900 @@ -26,7 +26,7 @@ lst[-1] += c continue if qt: - if c in qts: + if c == qt: lst[-1] += c lst += [''] qt = None @@ -198,7 +198,7 @@ def dump_dict(dic, idt): lines = [] for (k, v) in dic.items(): - s = dump_other(k, idt) + ':' + s = dump_other(k, idt, from_dic='l') + ':' if type(v) == list and v: lines.append(s) s = dump_list(v, idt) @@ -206,7 +206,7 @@ lines.append(s) s = dump_dict(v, idt+2) else: - s += ' ' + dump_value(v, dic_right=True) + s += ' ' + dump_value(v, from_dic='r') lines.append(s) return '\n'.join(lines) @@ -228,11 +228,16 @@ return False -def dump_value(obj, dic_right=False): +def sel_quote(q2, q1, cl, s, obj): + if q2 or q1 or cl or get_value(s) != obj or need_quote(s): + return '"' if q1 else "'" + return '' + +def dump_value(obj, from_dic=None): if type(obj) == str: s = obj (q2, q1, cl) = map( lambda t: t in s, ('"', "'", ':') ) - if cl and dic_right: + if cl and from_dic == 'r': t = s cl = False while ':' in t: @@ -243,8 +248,13 @@ if q2 and q1: s = s.replace("'", "\\'") q1 = False - if q2 or q1 or cl or get_value(s) != obj or need_quote(s): - qt = '"' if q1 else "'" + if from_dic != 'l': + if q1 and not sel_quote(q2, False, cl, s, obj): + q1 = False + if q2 and not sel_quote(False, q1, cl, s, obj): + q2 = False + qt = sel_quote(q2, q1, cl, s, obj) + if qt: s = qt + s + qt return s @@ -263,8 +273,8 @@ return '{}'.format(obj) -def dump_other(obj, idt): - return (' ' * idt) + dump_value(obj) +def dump_other(obj, idt, from_dic=None): + return (' ' * idt) + dump_value(obj, from_dic) def dump_obj(obj, idt): t = type(obj)