25 Commits

Author SHA1 Message Date
Tomohiro
87dc1820c0 fixes overflow defect when compiled with js backend (#25132)
Follow up to https://github.com/nim-lang/Nim/pull/25126.
This fixes overflow defect when `tests/stdlib/tstrutils.nim` was
compiled with js backend.
2025-09-01 18:29:58 +02:00
Tomohiro
065c4b443b fixes #25125 (#25126)
`strutils.formatSize` returns correct strings from large values close to
`int64.high`.
Round down `bytes` when it is converted to float.
2025-08-28 21:56:46 +02:00
Zoom
909f3b8b79 [feature] stdlib: strutils.multiReplace for character sets (#24805)
Multiple replacements based on character sets in a single pass. Useful
for string sanitation. Follows existing `multiReplace` semantics.

Note: initially copied the substring version logic with a `while` and a
named block break, but Godbolt showed it had produced slightly larger
assembly using higher registers than the final version.

- [x] Tests
- [x] changelog.md
2025-03-25 07:40:01 +01:00
metagn
041098e882 clean up stdlib with --jsbigint64 (#24255)
refs #6978, refs #6752, refs #21613, refs #24234

The `jsNoInt64`, `whenHasBigInt64`, `whenJsNoBigInt64` templates are
replaced with bool constants to use with `when`. Weird that I didn't do
this in the first place.

The `whenJsNoBigInt64` template was also slightly misleading. The first
branch was compiled for both no bigint64 on JS as well as on C/C++. It
seems only `trandom` depended on this by mistake.

The workaround for #6752 added in #6978 to `times` is also removed with
`--jsbigint64:on`, but #24233 was also encountered with this, so this PR
depends on #24234.
2024-10-19 16:40:28 +02:00
c-blake
4faa15f3ad Replacement PR for https://github.com/nim-lang/Nim/pull/23779 that (#23793)
makes new hash the default, with an opt-out (& js-no-big-int) define.
Also update changelog (& fix one typo).

Only really expect the chronos hash-order sensitive test to fail until
they merge that PR and tag a new release.
2024-07-07 12:51:42 +02:00
ringabout
04f3df4c87 fixes testament matrix doesn't work with other backends which left many JS tests untested (#23592)
Targets are not changes, which means the C binary is actually tested for
JS backend
2024-05-14 11:33:08 +02:00
metagn
51cb493b22 make parseEnum skip type aliases for enum type sym (#22727)
fixes #22726
2023-09-19 09:14:55 +02:00
Zoom
5e529b3bfa strutils.split/rsplit now return src on an empty sep (#22136)
This is a rebase of an earlier rejected PR. Following the discussion
around it, this commit provides a valid output for and edge case
of an empty separator for `split` and `rsplit` routines. The empty
separator is interpreted as "split by no separators" and the initial
string is returned. This is consistent with the behaviour of the
`set[char]` version of `split`/`rsplit` routines and unifies them all.

Compared to a commit merged earlier, this one has a benefit of
not using assertions that will be removed in release builds
and thus still not preventing possible infinite loops (which was the
earlier behaviour for this edge case for separator of type `string`).

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-06-21 08:52:33 +02:00
ringabout
40f88da90b alternative to #21914; split, rsplit now forbid an empty separator (#21961) 2023-05-30 13:40:09 +02:00
ringabout
4fa86422c0 stdlib tests now check refc too (#21664)
* stdlib tests now check refc too

* typo

* fixes line numbers

* disable cpp

* do not touch
2023-04-21 15:37:58 +02:00
metagn
f05387045d int64/uint64 as bigint in JS (#21613)
* int64/uint64 as bigint in JS

* fix CI

* convert to compile option

* fix lie

* smaller diff, changelog entry
2023-04-11 21:20:20 +02:00
ee7
10355cb48a strutils, rstgen: avoid deprecated strutils.delete (#20488)
The strutils `delete` func with signature

    func delete*(s: var string, first, last: int)

was deprecated in adba5eb45e, in favor of one with signature

    func delete*(s: var string, slice: Slice[int])

However, a few procedures still used the deprecated form. This commit
updates them, resolving these deprecation warnings:

    rstgen.nim(766, 12) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated]
    strutils.nim(1651, 19) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated]
    strutils.nim(1679, 7) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated]
    strutils.nim(2472, 7) Warning: use `delete(s, first..last)`; delete is deprecated [Deprecated]

Before this commit:

- `trimZeros` called `s.delete(i+1, i)` for an input that lacks a
  trailing zero (like "1.23").

- `removePrefix*(s: var string, prefix: string)` called
  `s.delete(0, -1)` when the prefix was the empty string.

which did not modify `s`, nor raise an error. But the newer slice
`delete` raises an `IndexDefect` when the start of the slice is greater
than the end, so we avoid calling the new `delete` for such a case.

Recall that exceptions inheriting from `system.Defect` are not tracked
with the `.raises: []` exception tracking mechanism [1], so this commit
does not break existing code like:

    proc foo {.raises: [].} =
      var s = "abc1.20"
      s.removePrefix("abc")
      s.trimZeros()
      doAssert s == "1.2"

The `strutils.delete` deprecation was motivated by a problem with
`system.delete` [2][3]:

    `system.delete` had surprising behavior when the index passed to it
    was out of bounds (it would delete the last entry then). Compile
    with `-d:nimStrictDelete` so that an index error is produced
    instead.

    Be aware however that your code might depend on this quirky behavior
    so a review process is required on your part before you can use
    `-d:nimStrictDelete`. To make this review easier, use the
    `-d:nimAuditDelete` switch, which pretends that `system.delete` is
    deprecated so that it is easier to see where it was used in your
    code.

    `-d:nimStrictDelete` will become the default in upcoming versions.

A similar deprecation happened with `sequtils.delete` [4], but that
deprecated form is already not used in this repo.

[1] https://github.com/nim-lang/Nim/blob/2dec69fe5aa6/doc/manual.md#exception-tracking
[2] https://github.com/nim-lang/Nim/blob/2dec69fe5aa6/changelogs/changelog_1_6_0.md#system
[3] https://github.com/nim-lang/Nim/commit/92cb76571432
[4] https://github.com/nim-lang/Nim/commit/1d6863a7899f
2022-10-05 13:57:17 -04:00
ringabout
65c2518d5c fix #19500; remove find optimization [backport: 1.6] (#19714)
* remove find optimization

close #19500

* save find to std

* add simple tests

* Apply suggestions from code review

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

Co-authored-by: sandytypical <43030857+xflywind@users.noreply.github.com>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
2022-09-28 16:05:41 -04:00
ringabout
3d2f0e2c7c make more standard libraries work with nimPreviewSlimSystem (#20343)
* make more standard libraries work with `nimPreviewSlimSystem`

* typo

* part two

* Delete specutils.nim

* fixes more tests

* more fixes

* fixes tests

* fixes three more tests

* add formatfloat import

* fix

* last
2022-09-27 20:06:23 +02:00
n5m
9ca88a1889 fix #18128 rfind on empty needle returns rightmost index (#20430)
rfind on empty needle returns haystack len
2022-09-26 15:39:22 -04:00
Zoom
b024a45163 Fix find routines' api to default to last=-1 (#19761)
This changes the default for the `last` parameter of various `find`
routines from `0` to `-1`. Previous default prevents limiting the search
to the first character. This is a logic error, as full text search was
performed for 2 *valid* values of `last`: `0` and `last.high()`.

Adds an overload for `initSkipTable` which returns a newly initialized
table. This encapsulates every single usage of a `var`-acting original
func in this module.

Co-authored-by: flywind <xzsflywind@gmail.com>
2022-06-07 10:35:06 +02:00
flywind
33bfe5b12c fix parseEnum cannot parse enum with const fields (#19466)
fix #19463
2022-02-04 13:47:03 +01:00
Tomasz Kramkowski
ee703c5db4 Fix nimIdentNormalize, fixes #19067 (#19068)
* Make nimIdentNormalize return "" when passed ""; fixes #19067

Fixes #19067

* Add tests for nimIdentNormalize
2021-10-29 11:39:41 +02:00
Timothee Cour
adba5eb45e deprecate strutils.delete and add an overload with saner semantics consistent with sequtils.delete; follows #18487 (#18510) 2021-07-18 11:10:08 +02:00
n5m
4ee6eddad4 add string-string tests for find and rfind (#18137)
* add string-string tests for find and rfind

* remove duplicate test cases

* remove more redundant test cases
2021-06-02 16:19:30 +02:00
n5m
064fe18de6 improve fast returns of find and rfind (#18127) 2021-05-31 11:20:15 +02:00
Timothee Cour
d19e4310dc std/hashes: hash(ref|ptr|pointer) + other improvements (#17731) 2021-04-16 14:21:26 +02:00
flywind
6d442a40a6 use doAssert in tests (#16486) 2020-12-28 14:13:21 +01:00
flywind
417c2509c4 strip minor improvement (#16444)
* strip minor improvement
* add more tests
* Update tests/stdlib/tstrutils.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
2020-12-23 11:57:48 +01:00
Timothee Cour
9ede956662 improve tstrutils: test c, cpp, js, vm; cleanups (#16357)
* renamed:    tests/stdlib/tstrutil.nim -> tests/stdlib/tstrutils.nim

* improve test

* enable tstrutils for js, vm
2020-12-15 14:55:41 +01:00