tstringitems.nim 1.9 KB

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