Skip to content

Commit

Permalink
add sink and lent annotations for xmltree and streams (nim-lang#18037)
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m authored and PMunch committed Mar 28, 2022
1 parent 2b4ed43 commit 0f25a09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/pure/streams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ else: # after 1.3 or JS not defined
var s = StringStream(s)
s.data = ""

proc newStringStream*(s: string = ""): owned StringStream =
proc newStringStream*(s: sink string = ""): owned StringStream =
## Creates a new stream from the string `s`.
##
## See also:
Expand Down
30 changes: 15 additions & 15 deletions lib/pure/xmltree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ proc newXmlNode(kind: XmlNodeKind): XmlNode =
## Creates a new ``XmlNode``.
result = XmlNode(k: kind)

proc newElement*(tag: string): XmlNode =
proc newElement*(tag: sink string): XmlNode =
## Creates a new ``XmlNode`` of kind ``xnElement`` with the given `tag`.
##
## See also:
Expand All @@ -89,7 +89,7 @@ proc newElement*(tag: string): XmlNode =
result.s = @[]
# init attributes lazily to save memory

proc newText*(text: string): XmlNode =
proc newText*(text: sink string): XmlNode =
## Creates a new ``XmlNode`` of kind ``xnText`` with the text `text`.
runnableExamples:
var b = newText("my text")
Expand All @@ -99,13 +99,13 @@ proc newText*(text: string): XmlNode =
result = newXmlNode(xnText)
result.fText = text

proc newVerbatimText*(text: string): XmlNode {.since: (1, 3).} =
proc newVerbatimText*(text: sink string): XmlNode {.since: (1, 3).} =
## Creates a new ``XmlNode`` of kind ``xnVerbatimText`` with the text `text`.
## **Since**: Version 1.3.
result = newXmlNode(xnVerbatimText)
result.fText = text

proc newComment*(comment: string): XmlNode =
proc newComment*(comment: sink string): XmlNode =
## Creates a new ``XmlNode`` of kind ``xnComment`` with the text `comment`.
runnableExamples:
var c = newComment("my comment")
Expand All @@ -115,7 +115,7 @@ proc newComment*(comment: string): XmlNode =
result = newXmlNode(xnComment)
result.fText = comment

proc newCData*(cdata: string): XmlNode =
proc newCData*(cdata: sink string): XmlNode =
## Creates a new ``XmlNode`` of kind ``xnCData`` with the text `cdata`.
runnableExamples:
var d = newCData("my cdata")
Expand All @@ -135,7 +135,7 @@ proc newEntity*(entity: string): XmlNode =
result = newXmlNode(xnEntity)
result.fText = entity

proc newXmlTree*(tag: string, children: openArray[XmlNode],
proc newXmlTree*(tag: sink string, children: openArray[XmlNode],
attributes: XmlAttributes = nil): XmlNode =
## Creates a new XML tree with `tag`, `children` and `attributes`.
##
Expand All @@ -151,7 +151,7 @@ proc newXmlTree*(tag: string, children: openArray[XmlNode],
h.add newEntity("some entity")
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
let k = newXmlTree("treeTag", [g, h], att)

doAssert $k == """<treeTag key1="first value" key2="second value">
<myTag>some text<!-- this is comment --></myTag>
<secondTag>&some entity;</secondTag>
Expand All @@ -163,7 +163,7 @@ proc newXmlTree*(tag: string, children: openArray[XmlNode],
for i in 0..children.len-1: result.s[i] = children[i]
result.fAttr = attributes

proc text*(n: XmlNode): string {.inline.} =
proc text*(n: XmlNode): lent string {.inline.} =
## Gets the associated text with the node `n`.
##
## `n` can be a CDATA, Text, comment, or entity node.
Expand All @@ -181,7 +181,7 @@ proc text*(n: XmlNode): string {.inline.} =
assert n.k in {xnText, xnComment, xnCData, xnEntity}
result = n.fText

proc `text=`*(n: XmlNode, text: string){.inline.} =
proc `text=`*(n: XmlNode, text: sink string) {.inline.} =
## Sets the associated text with the node `n`.
##
## `n` can be a CDATA, Text, comment, or entity node.
Expand All @@ -199,7 +199,7 @@ proc `text=`*(n: XmlNode, text: string){.inline.} =
assert n.k in {xnText, xnComment, xnCData, xnEntity}
n.fText = text

proc tag*(n: XmlNode): string {.inline.} =
proc tag*(n: XmlNode): lent string {.inline.} =
## Gets the tag name of `n`.
##
## `n` has to be an ``xnElement`` node.
Expand All @@ -220,7 +220,7 @@ proc tag*(n: XmlNode): string {.inline.} =
assert n.k == xnElement
result = n.fTag

proc `tag=`*(n: XmlNode, tag: string) {.inline.} =
proc `tag=`*(n: XmlNode, tag: sink string) {.inline.} =
## Sets the tag name of `n`.
##
## `n` has to be an ``xnElement`` node.
Expand Down Expand Up @@ -389,13 +389,13 @@ proc clear*(n: var XmlNode) =
var g = newElement("myTag")
g.add newText("some text")
g.add newComment("this is comment")

var h = newElement("secondTag")
h.add newEntity("some entity")

let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
var k = newXmlTree("treeTag", [g, h], att)

doAssert $k == """<treeTag key1="first value" key2="second value">
<myTag>some text<!-- this is comment --></myTag>
<secondTag>&some entity;</secondTag>
Expand Down Expand Up @@ -447,7 +447,7 @@ proc toXmlAttributes*(keyValuePairs: varargs[tuple[key,
let att = {"key1": "first value", "key2": "second value"}.toXmlAttributes
var j = newElement("myTag")
j.attrs = att

doAssert $j == """<myTag key1="first value" key2="second value" />"""

newStringTable(keyValuePairs)
Expand Down

0 comments on commit 0f25a09

Please sign in to comment.