File tree Expand file tree Collapse file tree 4 files changed +14
-1
lines changed Expand file tree Collapse file tree 4 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -327,6 +327,10 @@ Converts a Nim string to a C string.
327327
328328Prepares a string for mutation. String literals are "copy on write", so you need to call ` prepareMutation ` before modifying strings via ` addr ` .
329329
330+ ####prepareMutationAt
331+
332+ Prepares the given string for mutation and returns an addressable
333+ reference to the character at index ` i ` .
330334
331335####&
332336
Original file line number Diff line number Diff line change @@ -287,6 +287,10 @@ proc prepareMutation*(s: var string) =
287287 s.i = EmptyI
288288 s.a = a # also do this for `a == nil`
289289
290+ proc prepareMutationAt * (s: var string ; i: int ): var char {.requires : (i < len (s) and i >= 0 ), inline .} =
291+ prepareMutation (s)
292+ result = s.a[i]
293+
290294proc newString * (len: int ): string =
291295 let a = cast [StrData ](alloc (len))
292296 if a != nil :
Original file line number Diff line number Diff line change @@ -383,7 +383,7 @@ proc trProcDecl(c: var Context; n: var Cursor) =
383383 takeParRi c, n
384384 else :
385385 var body = n
386- trSons c, n, c.r.returnExpects
386+ tr c, n, c.r.returnExpects
387387 if c.r.dangerousLocations.len > 0 :
388388 checkForDangerousLocations c, body
389389 takeParRi c, n
Original file line number Diff line number Diff line change @@ -143,3 +143,8 @@ block: # issue #1444
143143 assert substr (" abc" , 3 , 1 ) == " "
144144 assert substr (" abc" , 3 , 2 ) == " "
145145 assert substr (" abc" , 3 , 3 ) == " "
146+
147+ block :
148+ var s = " 12234"
149+ var m = prepareMutationAt (s, 1 )
150+ assert m == '2'
You can’t perform that action at this time.
0 commit comments