Jim Tcl
Check-in [54894f621d]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:lreplace: Implement TIP #505

More consistent behaviour of replacing past end of list

Signed-off-by: Steve Bennett <steveb@workware.net.au>

Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 54894f621d029e8227cf9195625e0a7a38f2a052
User & Date: steveb@workware.net.au 2018-09-21 02:58:22
Context
2018-10-26
05:32
appveyor.yml: fix missing zlib1.dll in jimsh.zip check-in: aceabc1d7d user: steveb@workware.net.au tags: trunk
2018-09-21
02:58
lreplace: Implement TIP #505

More consistent behaviour of replacing past end of list

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: 54894f621d user: steveb@workware.net.au tags: trunk

02:57
file: Add microsecond resolution for mtime: mtimeus

Note that actual support is dependent upon the underlying operating system and filesystem.

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: f11e879168 user: steveb@workware.net.au tags: trunk

Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to jim.c.

12492
12493
12494
12495
12496
12497
12498
12499


12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
    last = JimRelToAbsIndex(len, last);
    JimRelToAbsRange(len, &first, &last, &rangeLen);

    /* Now construct a new list which consists of:
     * <elements before first> <supplied elements> <elements after last>
     */

    /* Check to see if trying to replace past the end of the list */


    if (first < len) {
        /* OK. Not past the end */
    }
    else if (len == 0) {
        /* Special for empty list, adjust first to 0 */
        first = 0;
    }
    else {
        Jim_SetResultString(interp, "list doesn't contain element ", -1);
        Jim_AppendObj(interp, Jim_GetResult(interp), argv[2]);
        return JIM_ERR;
    }

    /* Add the first set of elements */
    newListObj = Jim_NewListObj(interp, listObj->internalRep.listValue.ele, first);

    /* Add supplied elements */
    ListInsertElements(newListObj, -1, argc - 4, argv + 4);







|
>
>
|
<
<
<
<
|
<
<
<
<
<







12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502




12503





12504
12505
12506
12507
12508
12509
12510
    last = JimRelToAbsIndex(len, last);
    JimRelToAbsRange(len, &first, &last, &rangeLen);

    /* Now construct a new list which consists of:
     * <elements before first> <supplied elements> <elements after last>
     */

    /* Trying to replace past the end of the list means end of list
     * See TIP #505
     */
    if (first > len) {




        first = len;





    }

    /* Add the first set of elements */
    newListObj = Jim_NewListObj(interp, listObj->internalRep.listValue.ele, first);

    /* Add supplied elements */
    ListInsertElements(newListObj, -1, argc - 4, argv + 4);

Changes to tests/lreplace.test.

91
92
93
94
95
96
97
98












99
100
101
102
103
104
105
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129





































































130
131
132
133
test lreplace-1.26 {lreplace command} {
    catch {unset foo}
    set foo {a b}
    list [set foo [lreplace $foo end end]] \
        [set foo [lreplace $foo end end]] \
        [set foo [lreplace $foo end end]]
} {a {} {}}














test lreplace-2.1 {lreplace errors} {
    list [catch lreplace msg] $msg
} {1 {wrong # args: should be "lreplace list first last ?element ...?"}}
test lreplace-2.2 {lreplace errors} {
    list [catch {lreplace a b} msg] $msg
} {1 {wrong # args: should be "lreplace list first last ?element ...?"}}
................................................................................
    list [catch {lreplace x 10 x} msg] $msg
} {1 {bad index "x": must be integer?[+-]integer? or end?[+-]integer?}}
test lreplace-2.5 {lreplace errors} {
    list [catch {lreplace x 10 1x} msg] $msg
} {1 {bad index "1x": must be integer?[+-]integer? or end?[+-]integer?}}
test lreplace-2.6 {lreplace errors} {
    list [catch {lreplace x 3 2} msg] $msg
} {1 {list doesn't contain element 3}}
test lreplace-2.7 {lreplace errors} {
    list [catch {lreplace x 1 1} msg] $msg
} {1 {list doesn't contain element 1}}

test lreplace-3.1 {lreplace won't modify shared argument objects} {
    proc p {} {
        lreplace "a b c" 1 1 "x y"
        return "a b c"
    }
    p
} "a b c"






































































# cleanup
catch {unset foo}
::tcltest::cleanupTests
return







<
>
>
>
>
>
>
>
>
>
>
>
>







 







|


|









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




91
92
93
94
95
96
97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
test lreplace-1.26 {lreplace command} {
    catch {unset foo}
    set foo {a b}
    list [set foo [lreplace $foo end end]] \
        [set foo [lreplace $foo end end]] \
        [set foo [lreplace $foo end end]]
} {a {} {}}

test lreplace-1.27 {lreplace command} -body {
    lreplace x 1 1
} -result x
test lreplace-1.28 {lreplace command} -body {
    lreplace x 1 1 y
} -result {x y}
test lreplace-1.29 {lreplace command} -body {
    lreplace x 1 1 [error foo]
} -returnCodes 1 -result {foo}
test lreplace-1.30 {lreplace command} -body {
    lreplace {not {}alist} 0 0 [error foo]
} -returnCodes 1 -result {foo}

test lreplace-2.1 {lreplace errors} {
    list [catch lreplace msg] $msg
} {1 {wrong # args: should be "lreplace list first last ?element ...?"}}
test lreplace-2.2 {lreplace errors} {
    list [catch {lreplace a b} msg] $msg
} {1 {wrong # args: should be "lreplace list first last ?element ...?"}}
................................................................................
    list [catch {lreplace x 10 x} msg] $msg
} {1 {bad index "x": must be integer?[+-]integer? or end?[+-]integer?}}
test lreplace-2.5 {lreplace errors} {
    list [catch {lreplace x 10 1x} msg] $msg
} {1 {bad index "1x": must be integer?[+-]integer? or end?[+-]integer?}}
test lreplace-2.6 {lreplace errors} {
    list [catch {lreplace x 3 2} msg] $msg
} {0 x}
test lreplace-2.7 {lreplace errors} {
    list [catch {lreplace x 1 1} msg] $msg
} {0 x}

test lreplace-3.1 {lreplace won't modify shared argument objects} {
    proc p {} {
        lreplace "a b c" 1 1 "x y"
        return "a b c"
    }
    p
} "a b c"

test lreplace-4.1 {Bug ccc2c2cc98: lreplace edge case} {
    lreplace {} 1 1
} {}
test lreplace-4.2 {Bug ccc2c2cc98: lreplace edge case} {
    lreplace { } 1 1
} {}
test lreplace-4.3 {lreplace edge case} {
    lreplace {1 2 3} 2 0
} {1 2 3}
test lreplace-4.4 {lreplace edge case} {
    lreplace {1 2 3 4 5} 3 1
} {1 2 3 4 5}
test lreplace-4.5 {lreplace edge case} {
    lreplace {1 2 3 4 5} 3 0 _
} {1 2 3 _ 4 5}
test lreplace-4.6 {lreplace end-x: bug a4cb3f06c4} {
    lreplace {0 1 2 3 4} 0 end-2
} {3 4}
test lreplace-4.6.1 {lreplace end-x: bug a4cb3f06c4} {
    lreplace {0 1 2 3 4} 0 end-2 a b c
} {a b c 3 4}
test lreplace-4.7 {lreplace with two end-indexes: increasing} {
    lreplace {0 1 2 3 4} end-2 end-1
} {0 1 4}
test lreplace-4.7.1 {lreplace with two end-indexes: increasing} {
    lreplace {0 1 2 3 4} end-2 end-1 a b c
} {0 1 a b c 4}
test lreplace-4.8 {lreplace with two end-indexes: equal} {
    lreplace {0 1 2 3 4} end-2 end-2
} {0 1 3 4}
test lreplace-4.8.1 {lreplace with two end-indexes: equal} {
    lreplace {0 1 2 3 4} end-2 end-2 a b c
} {0 1 a b c 3 4}
test lreplace-4.9 {lreplace with two end-indexes: decreasing} {
    lreplace {0 1 2 3 4} end-2 end-3
} {0 1 2 3 4}
test lreplace-4.9.1 {lreplace with two end-indexes: decreasing} {
    lreplace {0 1 2 3 4} end-2 end-3 a b c
} {0 1 a b c 2 3 4}
test lreplace-4.10 {lreplace with two equal indexes} {
    lreplace {0 1 2 3 4} 2 2
} {0 1 3 4}
test lreplace-4.10.1 {lreplace with two equal indexes} {
    lreplace {0 1 2 3 4} 2 2 a b c
} {0 1 a b c 3 4}
test lreplace-4.11 {lreplace end index first} {
    lreplace {0 1 2 3 4} end-2 1 a b c
} {0 1 a b c 2 3 4}
test lreplace-4.12 {lreplace end index first} {
    lreplace {0 1 2 3 4} end-2 2 a b c
} {0 1 a b c 3 4}
test lreplace-4.13 {lreplace empty list} {
    lreplace {} 1 1 1
} 1
test lreplace-4.14 {lreplace empty list} {
    lreplace {} 2 2 2
} 2

test lreplace-5.1 {compiled lreplace: Bug 47ac84309b} {
    apply {x {
	lreplace $x end 0
    }} {a b c}
} {a b c}
test lreplace-5.2 {compiled lreplace: Bug 47ac84309b} {
    apply {x {
	lreplace $x end 0 A
    }} {a b c}
} {a b A c}

# cleanup
catch {unset foo}
::tcltest::cleanupTests
return