Skip to content

Commit

Permalink
Minor std/strscans improvements (#24566)
Browse files Browse the repository at this point in the history
#### Removes UnInit warnings when using `scanTuple` 

e.g. this would emit a warning
```nim
import std/strscans

proc main() =
  let (ok, number) = "123".scanTuple()
```

![image](https://github.com/user-attachments/assets/68170ac6-402d-48b0-b8b6-69e71f4b70ae)

#### Error for wrong type now points to the passed in variable

```nim
import std/strscans

var str: string
discard "123".scanf("$i", str)
```

it gave this warning before

![image](https://github.com/user-attachments/assets/096e56d2-0eb5-4c67-9725-25caa97afebd)
now it returns

![image](https://github.com/user-attachments/assets/736a4292-2f56-4cf3-a27a-677045377171)
  • Loading branch information
ire4ever1190 authored Dec 25, 2024
1 parent 65b2640 commit 5b9ff96
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/pure/strscans.nim
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ macro scanf*(input: string; pattern: static[string]; results: varargs[typed]): b
template at(s: string; i: int): char = (if i < s.len: s[i] else: '\0')
template matchError() =
error("type mismatch between pattern '$" & pattern[p] & "' (position: " & $p &
") and " & $getTypeInst(results[i]) & " var '" & repr(results[i]) & "'")
") and " & $getTypeInst(results[i]) & " var '" & repr(results[i]) & "'", results[i])

var i = 0
var p = 0
Expand Down Expand Up @@ -490,7 +490,7 @@ macro scanTuple*(input: untyped; pattern: static[string]; matcherTypes: varargs[
result = newStmtList()
template addVar(typ: string) =
let varIdent = ident("temp" & $arguments.len)
result.add(newNimNode(nnkVarSection).add(newIdentDefs(varIdent, ident(typ), newEmptyNode())))
result.add(newVarStmt(varIdent, newCall(ident"default", ident(typ))))
arguments.add(varIdent)
while p < pattern.len:
if pattern[p] == '$':
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/t8925.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
errormsg: "type mismatch between pattern '$$i' (position: 1) and HourRange var 'hour'"
file: "strscans.nim"
file: "t8925.nim"
"""

import strscans
Expand Down
11 changes: 11 additions & 0 deletions tests/stdlib/tstrscans_errs.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
discard """
cmd: "nim check $file"
"""

import std/strscans


block:
var strVar: string
discard "123".scanf("$i", strVar) #[tt.Error
^ type mismatch between pattern '$$i' (position: 1) and string var 'strVar']#

0 comments on commit 5b9ff96

Please sign in to comment.