diff -ur v17/ezyaml.c v18/ezyaml.c --- v17/ezyaml.c 2018-09-10 21:21:46.000000000 +0900 +++ v18/ezyaml.c 2018-09-12 22:20:36.000000000 +0900 @@ -146,15 +146,21 @@ } int -find_idx(char *s, char c) +find_idx(char *s, char *t) { int idx = 0; struct obj *lst = parse(s); + + s = lines_top_str(lst); + if( s && is_qts(s[0]) ){ + list_pop_top(lst); + idx += strlen(s); + } while( list_len(lst) > 0 ){ struct obj *str = list_pop_top(lst); s = obj_buf(str); - if( !is_qts(*s) && strchr(s, c) ){ - return idx + ( strchr(s, c) - s ); + if( strstr(s, t) ){ + return idx + ( strstr(s, t) - s ); } idx += strlen(s); } @@ -164,7 +170,13 @@ int find_colon_idx(char *s) { - return find_idx(s, ':'); + int n; + int idx = find_idx(s, ": "); + if(idx >= 0){ + return idx; + } + n = strlen(s); + return n > 0 && s[n-1] == ':' ? n-1 : -1; } #define TYP_OTHER (-1) @@ -543,7 +555,16 @@ } int -need_quote(char *s) +is_not_same_str(char *s, struct obj *obj) +{ + struct obj *v = get_value(s); + int r = !obj_eq(v, obj); + obj_del(v); + return r; +} + +int +need_quote(char *s, struct obj *obj, int from_dic) { char h, t; @@ -556,25 +577,13 @@ if( (h == '[' && t == ']') || (h == '{' && t == '}') ){ return 1; } - return 0; -} - -int -is_not_same_str(char *s, struct obj *obj) -{ - struct obj *v = get_value(s); - int r = !obj_eq(v, obj); - obj_del(v); - 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 ? "\"" : "'"; + if( is_not_same_str(s, obj) ){ + return 1; } - return NULL; + if( from_dic == 'l' && ( strstr(s, ": ") || s[ strlen(s)-1 ] == ':' ) ){ + return 1; + } + return 0; } struct obj * @@ -582,37 +591,16 @@ { 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 && from_dic == 'r'){ - char *t = s; - cl = 0; - while( ( t = strchr(t, ':') ) ){ - t++; - if(*t == '\0' || t[0] == ' '){ - cl = 1; - break; - } - } - } - if(q2 && q1){ - s = cp_replace(s, "'", "\\'"); - q1 = 0; - } - if(from_dic != 'l'){ - if(q1 && !sel_quote(q2, 0, cl, s, obj)){ + if( need_quote(s, obj, from_dic) ){ + char *qt; + int q2 = strchr(s, '"') != NULL; + int q1 = strchr(s, '\'') != NULL; + if(q2 && q1){ + s = cp_replace(s, "'", "\\'"); q1 = 0; } - if(q2 && !sel_quote(0, q1, cl, s, obj)){ - q2 = 0; - } - } - qt = sel_quote(q2, q1, cl, s, obj); - if(qt){ + qt = q1 ? "\"" : "'"; s = cp_add( strdup(qt), s ); s = cp_add( s, strdup(qt) ); } diff -ur v17/ezyaml.py v18/ezyaml.py --- v17/ezyaml.py 2018-09-10 21:21:43.000000000 +0900 +++ v18/ezyaml.py 2018-09-12 22:20:36.000000000 +0900 @@ -41,17 +41,22 @@ lst.pop() return lst -def find_idx(s, c): +def find_idx(s, t): idx = 0 lst = parse(s) + if lst and lst[0][:1] in qts: + idx += len( lst.pop(0) ) for s in lst: - if ( s[:1] not in qts ) and ( c in s ): - return idx + s.index(c) + if t in s: + return idx + s.index(t) idx += len(s) return -1 def find_colon_idx(s): - return find_idx(s, ':') + idx = find_idx(s, ': ') + if idx >= 0: + return idx + return len(s)-1 if s[-1:] == ':' else -1 def get_kind(s): if s.startswith('- '): @@ -218,7 +223,7 @@ lines.append(s) return '\n'.join(lines) -def need_quote(s): +def need_quote(s, obj, from_dic): if s in [''] + kwd_true + kwd_false: return True @@ -226,35 +231,23 @@ if ( s[:1], s[-1:] ) in hts: return True - return False + if get_value(s) != obj: + return True -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 '' + if from_dic == 'l' and (': ' in s or s[-1:] == ':'): + return True + + return False 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 from_dic == 'r': - t = s - cl = False - while ':' in t: - t = t[ t.index(':')+1 : ] - if not t or t[0] == ' ': - cl = True - break - if q2 and q1: - s = s.replace("'", "\\'") - q1 = False - if from_dic != 'l': - if q1 and not sel_quote(q2, False, cl, s, obj): + if need_quote(s, obj, from_dic): + (q2, q1) = map( lambda t: t in s, ('"', "'") ) + if q2 and q1: + s = s.replace("'", "\\'") 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: + qt = '"' if q1 else "'" s = qt + s + qt return s