aryRepArg redux, operand check, key grammar
[nedit-bw.git] / guaranty-order-for-numeric-keys.patch
blob73555d98e3922b4ce8afc46a5326ea38d5200976
1 From: Bert Wesarg <bert.wesarg@googlemail.com>
2 Subject: [PATCH] guaranty order for numeric array keys
4 Currently ther is no order guaranty for array keys in the for loops, this is
5 for numeric keys a little amberressing.
7 Use a new compare routine that takes care of numeric keys. This works also
8 for multi-dimensional keys with sub dimensional numeric keys.
10 Therefore, this do the right thing:
12 arr[10] = "ten"
13 arr[ 2] = "two"
14 arr[ 1] = "one"
16 for (k in arr) {
17 t_print(k "\n")
20 And this too:
22 arr[10,10] = "ten,ten"
23 arr[10, 2] = "ten,two"
24 arr[10, 1] = "ten,one"
25 arr[ 2,10] = "two,ten"
26 arr[ 2, 2] = "two,two"
27 arr[ 2, 1] = "two,one"
28 arr[ 1,10] = "one,ten"
29 arr[ 1, 2] = "one,two"
30 arr[ 1, 1] = "one,one"
32 for (k in arr) {
33 t_print(k "\n")
36 And this too:
38 arr["c",10] = "c,ten"
39 arr["c", 2] = "c,two"
40 arr["c", 1] = "c,one"
41 arr["b",10] = "b,ten"
42 arr["b", 2] = "b,two"
43 arr["b", 1] = "b,one"
44 arr["a",10] = "a,ten"
45 arr["a", 2] = "a,two"
46 arr["a", 1] = "a,one"
48 for (k in arr) {
49 t_print(k "\n")
52 There is one semantic change with this:
54 arr[1] = "one"
56 would be overridden by this:
58 arr["01"] = "zeroone"
60 ---
62 source/interpret.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
63 1 file changed, 76 insertions(+), 1 deletion(-)
65 diff --quilt old/source/interpret.c new/source/interpret.c
66 --- old/source/interpret.c
67 +++ new/source/interpret.c
68 @@ -2368,10 +2368,85 @@ static int arrayEntryCopyToNode(rbTreeNo
71 ** compare two array nodes returning an integer value similar to strcmp()
72 +**
73 +** take care of numeric keys and sort them right, also with multi dimensions
75 static int arrayEntryCompare(rbTreeNode *left, rbTreeNode *right)
77 - return(strcmp(((SparseArrayEntry *)left)->key, ((SparseArrayEntry *)right)->key));
78 + const char *keya = ((SparseArrayEntry *)left)->key;
79 + const char *keya_end = keya + strlen(keya);
80 + const char *keyb = ((SparseArrayEntry *)right)->key;
81 + const char *keyb_end = keyb + strlen(keyb);
82 + size_t seplen = strlen(ARRAY_DIM_SEP);
83 + int cmp;
85 + while (1) {
86 + long dima_len, numa;
87 + long dimb_len, numb;
88 + const char *dima_end = strstr(keya, ARRAY_DIM_SEP);
89 + const char *dimb_end = strstr(keyb, ARRAY_DIM_SEP);
90 + char *numa_end;
91 + char *numb_end;
93 + if (!dima_end) {
94 + dima_end = keya_end;
95 + }
96 + dima_len = dima_end - keya;
98 + if (!dimb_end) {
99 + dimb_end = keyb_end;
101 + dimb_len = dimb_end - keyb;
103 + /* try to parse a number */
104 + numa = strtol(keya, &numa_end, 10);
105 + numb = strtol(keyb, &numb_end, 10);
107 + /*
108 + ** if both strtol's eaten up till ARRAY_DIM_SEP, we have
109 + ** successfully parsded two numbers
110 + */
111 + if (numa_end == dima_end && numb_end == dimb_end) {
112 + /* successfully parsed numbers
113 + ** even empty parts
114 + */
116 + /* compare the numbers */
117 + cmp = (numa > numb) - (numa < numb);
118 + if (cmp != 0) {
119 + return cmp;
122 + } else {
124 + /* if the length differ, just compare the remainder */
125 + if (dima_len != dimb_len) {
126 + return strcmp(keya, keyb);
129 + /* compare this dimension */
130 + cmp = strncmp(keya, keyb, dima_len);
131 + if (cmp != 0) {
132 + return cmp;
136 + /* eat this dimension */
137 + keya = dima_end;
138 + keyb = dimb_end;
140 + /*
141 + ** break condition:
142 + ** at least one key is fully eaten, compare the remainder
143 + */
144 + if (dima_end == keya_end || dimb_end == keyb_end) {
145 + return strcmp(keya, keyb);
148 + /* both dim ends point to a ARRAY_DIM_SEP => skip */
149 + keya += seplen;
150 + keyb += seplen;