tstringitems.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. discard """
  2. output: '''Hello
  3. Hello
  4. c
  5. d
  6. e'''
  7. """
  8. block: # bug #2581
  9. const someVars = [ "Hello" ]
  10. var someVars2 = [ "Hello" ]
  11. proc getSomeVar: string =
  12. for i in someVars:
  13. if i == "Hello":
  14. result = i
  15. break
  16. proc getSomeVar2: string =
  17. for i in someVars2:
  18. if i == "Hello":
  19. result = i
  20. break
  21. echo getSomeVar()
  22. echo getSomeVar2()
  23. block: # Test compile-time binary data generation, invalid unicode
  24. proc signatureMaker(): string {. compiletime .} =
  25. const signatureBytes = [137, 80, 78, 71, 13, 10, 26, 10]
  26. result = ""
  27. for c in signatureBytes: result.add chr(c)
  28. const cSig = signatureMaker()
  29. var rSig = newString(8)
  30. rSig[0] = chr(137)
  31. rSig[1] = chr(80)
  32. rSig[2] = chr(78)
  33. rSig[3] = chr(71)
  34. rSig[4] = chr(13)
  35. rSig[5] = chr(10)
  36. rSig[6] = chr(26)
  37. rSig[7] = chr(10)
  38. doAssert(rSig == cSig)
  39. block: # Test unicode strings
  40. const constStr = "Привет!"
  41. var jsStr : cstring
  42. {.emit: """`jsStr` = "Привет!";""".}
  43. doAssert($jsStr == constStr)
  44. var runtimeStr = "При"
  45. runtimeStr &= "вет!"
  46. doAssert(runtimeStr == constStr)
  47. block: # Conversions from/to cstring
  48. proc stringSaysHelloInRussian(s: cstring): bool =
  49. {.emit: """`result` = (`s` === "Привет!");""".}
  50. doAssert(stringSaysHelloInRussian("Привет!"))
  51. const constStr = "Привет!"
  52. doAssert(stringSaysHelloInRussian(constStr))
  53. var rtStr = "Привет!"
  54. doAssert(stringSaysHelloInRussian(rtStr))
  55. block: # String case of
  56. const constStr = "Привет!"
  57. var s = "Привет!"
  58. case s
  59. of constStr: discard
  60. else: doAssert(false)
  61. case s
  62. of "Привет!": discard
  63. else: doAssert(false)
  64. block: # String cmp
  65. var a, b: string
  66. doAssert(cmp(a, b) == 0)
  67. doAssert(cmp("foo", "foo") == 0)
  68. doAssert(cmp("foobar", "foo") == 3)
  69. doAssert(cmp("foo", "foobar") == -3)
  70. doAssert(cmp("fooz", "foog") == 19)
  71. doAssert(cmp("foog", "fooz") == -19)
  72. proc main(x: openArray[char]) =
  73. for c in x: echo c
  74. main(toOpenArray(['a', 'b', 'c', 'd', 'e'], 2, 4))