tcursor_on_localvar.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. discard """
  2. output: '''Section: common
  3. Param: Floats1
  4. Section: local
  5. Param: Str
  6. Param: Bool
  7. Param: Floats2
  8. destroy Foo
  9. destroy Foo
  10. '''
  11. cmd: '''nim c --gc:arc $file'''
  12. """
  13. # bug #15325
  14. import tables
  15. import strutils
  16. const defaultSection = "***"
  17. type
  18. Config* = ref object
  19. table: OrderedTableRef[string, OrderedTable[string, string]]
  20. # ----------------------------------------------------------------------------------------------------------------------
  21. proc newConfig*(): Config =
  22. result = new(Config)
  23. result.table = newOrderedTable[string, OrderedTable[string, string]]()
  24. # ----------------------------------------------------------------------------------------------------------------------
  25. proc add*(self: Config, param, value, section: string) {.nosinks.} =
  26. let s = if section == "": defaultSection else: section
  27. if not self.table.contains(s):
  28. self.table[s] = initOrderedTable[string, string]()
  29. self.table[s][param] = value
  30. # ----------------------------------------------------------------------------------------------------------------------
  31. proc sections*(self: Config): seq[string] =
  32. result = @[]
  33. for i in self.table.keys:
  34. let s = if i == defaultSection: "" else: i
  35. result.add(s)
  36. # ----------------------------------------------------------------------------------------------------------------------
  37. proc params*(self: Config, section: string): seq[string] =
  38. result = @[]
  39. let s = if section == "": defaultSection else: section
  40. if self.table.contains(s):
  41. for i in self.table[s].keys:
  42. result.add(i)
  43. # ----------------------------------------------------------------------------------------------------------------------
  44. proc extract*(str, start, finish: string): string =
  45. let startPos = str.find(start)
  46. if startPos < 0:
  47. return ""
  48. let endPos = str.find(finish, startPos)
  49. if endPos < 0:
  50. return ""
  51. return str[startPos + start.len() ..< endPos]
  52. # ----------------------------------------------------------------------------------------------------------------------
  53. proc loadString*(self: Config, text: string): tuple[valid: bool, errorInLine: int] {.discardable.} =
  54. self.table.clear()
  55. var data = ""
  56. data = text
  57. var
  58. actualSection = ""
  59. lineCount = 0
  60. for i in splitLines(data):
  61. lineCount += 1
  62. var line = strip(i)
  63. if line.len() == 0:
  64. continue
  65. if line[0] == '#' or line[0] == ';':
  66. continue
  67. if line[0] == '[':
  68. let section = strip(extract(line, "[", "]"))
  69. if section.len() != 0:
  70. actualSection = section
  71. else:
  72. self.table.clear()
  73. return (false, lineCount)
  74. else:
  75. let equal = find(line, '=')
  76. if equal <= 0:
  77. self.table.clear()
  78. return (false, lineCount)
  79. else:
  80. let
  81. param = strip(line[0 .. equal - 1])
  82. value = strip(line[equal + 1 .. ^1])
  83. if param.len() == 0:
  84. self.table.clear()
  85. return (false, lineCount)
  86. else:
  87. self.add(param, value, actualSection)
  88. return (true, 0)
  89. # ----------------------------------------------------------------------------------------------------------------------
  90. when isMainModule:
  91. var cfg = newConfig()
  92. cfg.loadString("[common]\nFloats1 = 1,2,3\n[local]\nStr = \"String...\"\nBool = true\nFloats2 = 4, 5, 6\n")
  93. for s in cfg.sections():
  94. echo "Section: " & s
  95. for p in cfg.params(s):
  96. echo " Param: " & p
  97. # bug #16437
  98. type
  99. Foo = object
  100. FooRef = ref Foo
  101. Bar = ref object
  102. f: FooRef
  103. proc `=destroy`(o: var Foo) =
  104. echo "destroy Foo"
  105. proc testMe(x: Bar) =
  106. var c = (if x != nil: x.f else: nil)
  107. assert c != nil
  108. proc main =
  109. var b = Bar(f: FooRef())
  110. testMe(b)
  111. main()
  112. proc testMe2(x: Bar) =
  113. var c: FooRef
  114. c = (if x != nil: x.f else: nil)
  115. assert c != nil
  116. proc main2 =
  117. var b = Bar(f: FooRef())
  118. testMe2(b)
  119. main2()