From a720539f5e5b3abe504b31fbf5a0fc85ebac0b0d Mon Sep 17 00:00:00 2001 From: Araq Date: Sun, 26 Nov 2017 03:24:59 +0100 Subject: fixes system.runnableExamples; strutils makes use of runnableExamples --- compiler/semexprs.nim | 4 +- lib/pure/strutils.nim | 141 +++++++++++++++++++++++--------------------------- 2 files changed, 68 insertions(+), 77 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 380b367bc..4942ef385 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1854,8 +1854,8 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode = if c.runnableExamples == nil: c.runnableExamples = newTree(nkStmtList, newTree(nkImportStmt, newStrNode(nkStrLit, expandFilename(inp)))) - c.runnableExamples.add newTree(nkBlockStmt, emptyNode, n.lastSon) - result = n + c.runnableExamples.add newTree(nkBlockStmt, emptyNode, copyTree n.lastSon) + result = setMs(n, s) else: result = emptyNode else: diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index d773cc7d8..4ac40d8b4 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1305,14 +1305,13 @@ proc addSep*(dest: var string, sep = ", ", startLen: Natural = 0) ## This is often useful for generating some code where the items need to ## be *separated* by `sep`. `sep` is only added if `dest` is longer than ## `startLen`. The following example creates a string describing - ## an array of integers: - ## - ## .. code-block:: nim - ## var arr = "[" - ## for x in items([2, 3, 5, 7, 11]): - ## addSep(arr, startLen=len("[")) - ## add(arr, $x) - ## add(arr, "]") + ## an array of integers. + runnableExamples: + var arr = "[" + for x in items([2, 3, 5, 7, 11]): + addSep(arr, startLen=len("[")) + add(arr, $x) + add(arr, "]") if dest.len > startLen: add(dest, sep) proc allCharsInSet*(s: string, theSet: set[char]): bool = @@ -1730,7 +1729,9 @@ proc insertSep*(s: string, sep = '_', digits = 3): string {.noSideEffect, ## ## Even though the algorithm works with any string `s`, it is only useful ## if `s` contains a number. - ## Example: ``insertSep("1000000") == "1_000_000"`` + runnableExamples: + doAssert insertSep("1000000") == "1_000_000" + var L = (s.len-1) div digits + s.len result = newString(L) var j = 0 @@ -1818,6 +1819,8 @@ proc validIdentifier*(s: string): bool {.noSideEffect, ## ## A valid identifier starts with a character of the set `IdentStartChars` ## and is followed by any number of characters of the set `IdentChars`. + runnableExamples: + doAssert "abc_def08".validIdentifier if s[0] in IdentStartChars: for i in 1..s.len-1: if s[i] notin IdentChars: return false @@ -1828,7 +1831,7 @@ proc editDistance*(a, b: string): int {.noSideEffect, ## Returns the edit distance between `a` and `b`. ## ## This uses the `Levenshtein`:idx: distance algorithm with only a linear - ## memory overhead. This implementation is highly optimized! + ## memory overhead. var len1 = a.len var len2 = b.len if len1 > len2: @@ -2007,16 +2010,11 @@ proc formatFloat*(f: float, format: FloatFormatMode = ffDefault, ## after the decimal point for Nim's ``float`` type. ## ## If ``precision == -1``, it tries to format it nicely. - ## - ## Examples: - ## - ## .. code-block:: nim - ## - ## let x = 123.456 - ## doAssert x.formatFloat() == "123.4560000000000" - ## doAssert x.formatFloat(ffDecimal, 4) == "123.4560" - ## doAssert x.formatFloat(ffScientific, 2) == "1.23e+02" - ## + runnableExamples: + let x = 123.456 + doAssert x.formatFloat() == "123.4560000000000" + doAssert x.formatFloat(ffDecimal, 4) == "123.4560" + doAssert x.formatFloat(ffScientific, 2) == "1.23e+02" result = formatBiggestFloat(f, format, precision, decimalSep) proc trimZeros*(x: var string) {.noSideEffect.} = @@ -2051,18 +2049,13 @@ proc formatSize*(bytes: int64, ## ## `includeSpace` can be set to true to include the (SI preferred) space ## between the number and the unit (e.g. 1 KiB). - ## - ## Examples: - ## - ## .. code-block:: nim - ## - ## formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB" - ## formatSize((2.234*1024*1024).int) == "2.234MiB" - ## formatSize(4096, includeSpace=true) == "4 KiB" - ## formatSize(4096, prefix=bpColloquial, includeSpace=true) == "4 kB" - ## formatSize(4096) == "4KiB" - ## formatSize(5_378_934, prefix=bpColloquial, decimalSep=',') == "5,13MB" - ## + runnableExamples: + doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB" + doAssert formatSize((2.234*1024*1024).int) == "2.234MiB" + doAssert formatSize(4096, includeSpace=true) == "4 KiB" + doAssert formatSize(4096, prefix=bpColloquial, includeSpace=true) == "4 kB" + doAssert formatSize(4096) == "4KiB" + doAssert formatSize(5_378_934, prefix=bpColloquial, decimalSep=',') == "5,13MB" const iecPrefixes = ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"] const collPrefixes = ["", "k", "M", "G", "T", "P", "E", "Z", "Y"] var @@ -2156,7 +2149,7 @@ proc formatEng*(f: BiggestFloat, ## formatEng(4100, unit="V") == "4.1e3 V" ## formatEng(4100, unit="") == "4.1e3 " # Space with unit="" ## - ## `decimalSep` is used as the decimal separator + ## `decimalSep` is used as the decimal separator. var absolute: BiggestFloat significand: BiggestFloat @@ -2369,17 +2362,16 @@ proc removeSuffix*(s: var string, chars: set[char] = Newlines) {. rtl, extern: "nsuRemoveSuffixCharSet".} = ## Removes all characters from `chars` from the end of the string `s` ## (in-place). - ## - ## .. code-block:: nim - ## var userInput = "Hello World!*~\r\n" - ## userInput.removeSuffix - ## doAssert userInput == "Hello World!*~" - ## userInput.removeSuffix({'~', '*'}) - ## doAssert userInput == "Hello World!" - ## - ## var otherInput = "Hello!?!" - ## otherInput.removeSuffix({'!', '?'}) - ## doAssert otherInput == "Hello" + runnableExamples: + var userInput = "Hello World!*~\r\n" + userInput.removeSuffix + doAssert userInput == "Hello World!*~" + userInput.removeSuffix({'~', '*'}) + doAssert userInput == "Hello World!" + + var otherInput = "Hello!?!" + otherInput.removeSuffix({'!', '?'}) + doAssert otherInput == "Hello" if s.len == 0: return var last = s.high while last > -1 and s[last] in chars: last -= 1 @@ -2390,24 +2382,23 @@ proc removeSuffix*(s: var string, c: char) {. ## Removes all occurrences of a single character (in-place) from the end ## of a string. ## - ## .. code-block:: nim - ## var table = "users" - ## table.removeSuffix('s') - ## doAssert table == "user" - ## - ## var dots = "Trailing dots......." - ## dots.removeSuffix('.') - ## doAssert dots == "Trailing dots" + runnableExamples: + var table = "users" + table.removeSuffix('s') + doAssert table == "user" + + var dots = "Trailing dots......." + dots.removeSuffix('.') + doAssert dots == "Trailing dots" removeSuffix(s, chars = {c}) proc removeSuffix*(s: var string, suffix: string) {. rtl, extern: "nsuRemoveSuffixString".} = ## Remove the first matching suffix (in-place) from a string. - ## - ## .. code-block:: nim - ## var answers = "yeses" - ## answers.removeSuffix("es") - ## doAssert answers == "yes" + runnableExamples: + var answers = "yeses" + answers.removeSuffix("es") + doAssert answers == "yes" var newLen = s.len if s.endsWith(suffix): newLen -= len(suffix) @@ -2418,16 +2409,16 @@ proc removePrefix*(s: var string, chars: set[char] = Newlines) {. ## Removes all characters from `chars` from the start of the string `s` ## (in-place). ## - ## .. code-block:: nim - ## var userInput = "\r\n*~Hello World!" - ## userInput.removePrefix - ## doAssert userInput == "*~Hello World!" - ## userInput.removePrefix({'~', '*'}) - ## doAssert userInput == "Hello World!" - ## - ## var otherInput = "?!?Hello!?!" - ## otherInput.removePrefix({'!', '?'}) - ## doAssert otherInput == "Hello!?!" + runnableExamples: + var userInput = "\r\n*~Hello World!" + userInput.removePrefix + doAssert userInput == "*~Hello World!" + userInput.removePrefix({'~', '*'}) + doAssert userInput == "Hello World!" + + var otherInput = "?!?Hello!?!" + otherInput.removePrefix({'!', '?'}) + doAssert otherInput == "Hello!?!" var start = 0 while start < s.len and s[start] in chars: start += 1 if start > 0: s.delete(0, start - 1) @@ -2437,20 +2428,20 @@ proc removePrefix*(s: var string, c: char) {. ## Removes all occurrences of a single character (in-place) from the start ## of a string. ## - ## .. code-block:: nim - ## var ident = "pControl" - ## ident.removePrefix('p') - ## doAssert ident == "Control" + runnableExamples: + var ident = "pControl" + ident.removePrefix('p') + doAssert ident == "Control" removePrefix(s, chars = {c}) proc removePrefix*(s: var string, prefix: string) {. rtl, extern: "nsuRemovePrefixString".} = ## Remove the first matching prefix (in-place) from a string. ## - ## .. code-block:: nim - ## var answers = "yesyes" - ## answers.removePrefix("yes") - ## doAssert answers == "yes" + runnableExamples: + var answers = "yesyes" + answers.removePrefix("yes") + doAssert answers == "yes" if s.startsWith(prefix): s.delete(0, prefix.len - 1) -- cgit 1.4.1-2-gfad0 /a> 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591