tcursor_on_localvar.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. for i in self.table.keys:
  33. let s = if i == defaultSection: "" else: i
  34. result.add(s)
  35. # ----------------------------------------------------------------------------------------------------------------------
  36. proc params*(self: Config, section: string): seq[string] =
  37. let s = if section == "": defaultSection else: section
  38. if self.table.contains(s):
  39. for i in self.table[s].keys:
  40. result.add(i)
  41. # ----------------------------------------------------------------------------------------------------------------------
  42. proc extract*(str, start, finish: string): string =
  43. let startPos = str.find(start)
  44. if startPos < 0:
  45. return ""
  46. let endPos = str.find(finish, startPos)
  47. if endPos < 0:
  48. return ""
  49. return str[startPos + start.len() ..< endPos]
  50. # ----------------------------------------------------------------------------------------------------------------------
  51. proc loadString*(self: Config, text: string): tuple[valid: bool, errorInLine: int] {.discardable.} =
  52. self.table.clear()
  53. var data = ""
  54. data = text
  55. var
  56. actualSection = ""
  57. lineCount = 0
  58. for i in splitLines(data):
  59. lineCount += 1
  60. var line = strip(i)
  61. if line.len() == 0:
  62. continue
  63. if line[0] == '#' or line[0] == ';':
  64. continue
  65. if line[0] == '[':
  66. let section = strip(extract(line, "[", "]"))
  67. if section.len() != 0:
  68. actualSection = section
  69. else:
  70. self.table.clear()
  71. return (false, lineCount)
  72. else:
  73. let equal = find(line, '=')
  74. if equal <= 0:
  75. self.table.clear()
  76. return (false, lineCount)
  77. else:
  78. let
  79. param = strip(line[0 .. equal - 1])
  80. value = strip(line[equal + 1 .. ^1])
  81. if param.len() == 0:
  82. self.table.clear()
  83. return (false, lineCount)
  84. else:
  85. self.add(param, value, actualSection)
  86. return (true, 0)
  87. # ----------------------------------------------------------------------------------------------------------------------
  88. when isMainModule:
  89. var cfg = newConfig()
  90. cfg.loadString("[common]\nFloats1 = 1,2,3\n[local]\nStr = \"String...\"\nBool = true\nFloats2 = 4, 5, 6\n")
  91. for s in cfg.sections():
  92. echo "Section: " & s
  93. for p in cfg.params(s):
  94. echo " Param: " & p
  95. # bug #16437
  96. type
  97. Foo = object
  98. FooRef = ref Foo
  99. Bar = ref object
  100. f: FooRef
  101. proc `=destroy`(o: var Foo) =
  102. echo "destroy Foo"
  103. proc testMe(x: Bar) =
  104. var c = (if x != nil: x.f else: nil)
  105. assert c != nil
  106. proc main =
  107. var b = Bar(f: FooRef())
  108. testMe(b)
  109. main()
  110. proc testMe2(x: Bar) =
  111. var c: FooRef
  112. c = (if x != nil: x.f else: nil)
  113. assert c != nil
  114. proc main2 =
  115. var b = Bar(f: FooRef())
  116. testMe2(b)
  117. main2()