tslices.nim 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. discard """
  2. output: '''
  3. 456456
  4. 456456
  5. 456456
  6. Zugr5nd
  7. egerichtetd
  8. verichtetd
  9. '''
  10. """
  11. # Test the new slices.
  12. var mystr = "Abgrund"
  13. # mystr[..1] = "Zu" # deprecated
  14. mystr[0..1] = "Zu"
  15. mystr[4..4] = "5"
  16. type
  17. TEnum = enum e1, e2, e3, e4, e5, e6
  18. var myarr: array[TEnum, int] = [1, 2, 3, 4, 5, 6]
  19. myarr[e1..e3] = myarr[e4..e6]
  20. # myarr[..e3] = myarr[e4..e6] # deprecated
  21. myarr[0..e3] = myarr[e4..e6]
  22. for x in items(myarr): stdout.write(x)
  23. echo()
  24. var myarr2: array[0..5, int] = [1, 2, 3, 4, 5, 6]
  25. myarr2[0..2] = myarr2[3..5]
  26. for x in items(myarr2): stdout.write(x)
  27. echo()
  28. var myseq = @[1, 2, 3, 4, 5, 6]
  29. myseq[0..2] = myseq[^3 .. ^1]
  30. for x in items(myseq): stdout.write(x)
  31. echo()
  32. echo mystr
  33. mystr[4..4] = "u"
  34. # test full replacement
  35. # mystr[.. ^2] = "egerichtet" # deprecated
  36. mystr[0 .. ^2] = "egerichtet"
  37. echo mystr
  38. mystr[0..2] = "ve"
  39. echo mystr
  40. var s = "abcdef"
  41. s[1 .. ^2] = "xyz"
  42. assert s == "axyzf"
  43. # issue mentioned in PR #19219
  44. type Foo = distinct uint64
  45. # < here calls `pred` which used to cause a codegen error
  46. # `pred` compiles because distinct ordinals are considered ordinal
  47. const slice = 0 ..< 42.Foo