Commit Graph

5616 Commits

Author SHA1 Message Date
quimt
96c165304d conditional compilation of gcd(SomeInteger,SomeInteger) in std/math (#23773)
The most specific version of `gcd(int,int)` in `std/math` uses bitwise
comparisons from C compilers, which can't be borrowed on the js platform
in the web browser. Conditional compilation here should fix the issue
for this and downstream libraries such as `std/rationals` when compiling
to browser js as the backend.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-07-09 12:59:10 +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
Leon Lysak
ce75042a9d Update documentation for parseEnum in strutils.nim (#23804)
added small note regarding style insensitivity for parsing enums. the
casing of the first letter is still taken into account for this
function. was confused a little at first because when I read "style
insensitive manner" I thought it meant casing as well and ran into a
couple of `ValueError`'s because of it.
2024-07-06 22:51:15 +02:00
Gianmarco
96aba18963 Fixed issues when using std/parseopt in miscripts with cmdline = "" (#23785)
Using initOptParser with an empty cmdline (so that it gets the cmdline
from the command line) in nimscripts does not yield the expected
results.

Fixes #23774.
2024-07-02 08:49:21 +02:00
lit
43ee545789 Fix doc: '\c' '\L' in lexbase.nim (#23781)
- In lexbase.nim, `\c` `\L` were rendered as `c` `L`.
2024-07-01 20:47:39 +02:00
lit
a557e5a341 refine: strmisc.expandTabs better code structure (#23783)
After this pr, for a string with just 20 length and 6 `'\t'`, the time reduces by about 1.5%[^t].

Also, the code is clearer than the previous at some places.


[^t]: Generally speaking, this rate increases with length. I may test
for longer string later.
2024-07-01 20:47:08 +02:00
Mark Leyva
288d5c4ac3 fixes #5091; Ensure we don't wait on an exited process on Linux (#23743)
Fixes #5091.

Ensure we don't wait on an exited process on Linux
2024-07-01 11:42:11 +02:00
c-blake
e645120362 Add Farm Hash conditioned upon nimPreviewHashFarm as 64-bit Hash (#23735)
Unlike present Nim this actually fills `Hash` for `string` & related.

For the curious, note that `hashData` remains the aboriginal Nim string
hasher & `import hashes {.all.}` allows simultaneous test/time of {orig,
murmur, farm} on your favorite CPU & back end compiler.

Update tests also conditioned upon `nimPreviewHashFarm` so they should
pass either with or without that `define` on.

In `--jsbigint=on` mode, only the lower 32-bits of `Hash` match nimvm &
run-time values because `type Hash = int` and on JS int=int32, not int64
as for 64-bit Nim platforms. Due to the matching, `const` Table should
match run-time `Table` on all platforms.

To operate in `--jsbigint=off` mode is feasible but needs much "double
precision mul/xor/ror/shr-arithmetic"-style work. That is distracting &
also of questionable value since JS added BigInt in 2018, ringabout
added Nim support for it in 2021 & `nimPreviewHashFarm` is unlikely to
swap from an opt-in to an opt-out default before 2025..2026 which will
have given a backward looking time window of 7..8 years for deployment
platforms - reasonably generous.

Add a changelog entry for 2.2.
2024-06-19 06:49:57 +02:00
lit
2a658c64d8 fixes #23732, os.sleep(-1) now returns immediately (#23734)
fixes #23732
2024-06-18 17:39:34 +02:00
c-blake
8037bbe327 Fix non-exported memfiles.setFileSize to be able to shrink files on posix via memfiles.resize (#23717)
Fix non-exported `setFileSize` to take optional `oldSize` to (on posix)
shrink differently than it grows (`ftruncate` not `posix_fallocate`)
since it makes sense to assume the higher address space has already been
allocated there and include the old file size in the `proc resize` call.
Also, do not even try `setFileSize` in the first place unless the `open`
itself works by moving the call into the `if newFileSize != -1` branch.

Just cosmetics, also improve some old 2011 comments, note a logic diff
for callers using both `mappedSize` & `newFileSize` from windows branch
in case someone wants to fix that & simplify code formatting a little.
2024-06-14 08:23:26 +02:00
lit
3915fdc372 fixes #23513, parseutils.nim: parseInt's doc example. (#23561)
fixes #23513

Also, the old `runnableExample` is just a copy of `proc
parseInt(openArray[char], var int, int)` variant (in Line 1000).

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2024-06-12 10:13:38 +08:00
ringabout
87e56cabbb make std/options compatible with strictdefs (#23675) 2024-06-05 20:54:25 +02:00
ringabout
de4c7dfdd9 fixes #22798; Duplicate libraries linker warning (i.e., '-lm') on macOS (#23292)
fixes #22798

Per
https://stackoverflow.com/questions/33675638/gcc-link-the-math-library-by-default-in-c-on-mac-os-x
and
https://stackoverflow.com/questions/30694042/c-std-library-dont-appear-to-be-linked-in-object-file

> There's no separate math library on OSX. While a lot of systems ship
functions in the standard C math.h header in a separate math library,
OSX does not do that, it's part of the libSystem library, which is
always linked in.

required by https://github.com/nim-lang/Nim/pull/23290
2024-06-02 09:36:20 +08:00
Alexander Kernozhitsky
b172b34a24 Treat CJK Ideographs as letters in isAlpha() (#23651)
Because of the bug in `tools/parse_unicodedata.nim`, CJK Ideographs were
not considered letters in `isAlpha()`, even though they have category
Lo. This is because they are specified as range in `UnicodeData.txt`,
not as separate characters:

```
4E00;<CJK Ideograph, First>;Lo;0;L;;;;;N;;;;;
9FEF;<CJK Ideograph, Last>;Lo;0;L;;;;;N;;;;;
```

The parser was not prepared to parse such ranges and thus omitted almost
all CJK Ideographs from consideration.

To fix this, we need to consider ranges from `UnicodeData.txt` in
`tools/parse_unicodedata.nim`.
2024-05-29 06:42:07 +02:00
lit
b838d3ece1 doc(format): ospaths2,strutils: followup #23560 (#23629)
followup #23560
2024-05-20 19:18:28 +08:00
PHO
0ba932132e Support NetBSD/aarch64 (#23616)
I could trivially port Nim to NetBSD/aarch64 because it already
supported NetBSD and aarch64. I only needed to generate `c_code` for
this combination.
2024-05-16 23:22:49 +02:00
ringabout
1eb9aac2f7 adds Nim-related mimetypes back (#23589)
ref https://github.com/nim-lang/Nim/pull/23226
2024-05-10 10:30:24 +02:00
lit
2e3777d6f3 Improve strutils.rsplit doc, proc and iterator have oppose result order. (#23570)
[`rsplit
iterator`](https://nim-lang.org/docs/strutils.html#rsplit.i,string,char,int)
yields substring in reversed order,

while [`proc
rsplit`](https://nim-lang.org/docs/strutils.html#rsplit%2Cstring%2Cchar%2Cint)'s
order is not reversed, but its doc only declare ```
The same as the rsplit iterator, but is a func that returns a sequence
of substrings.
```
2024-05-10 10:30:06 +02:00
Angel Ezquerra
d8e1504ed1 Add Complex version of almostEqual function (#23549)
This adds a version of `almostEqual` (which was already available for
floats) thata works with `Complex[SomeFloat]`.

Proof that this is needed is that the first thing that the complex.nim
runnable examples block did before this commit was define (an
incomplete) `almostEqual` function that worked with complex values.
2024-05-08 14:53:01 -06:00
Marius Andra
e6f66e4d13 fixes 12381, HttpClient socket handle leak (#23575)
## Bug

Fixes https://github.com/nim-lang/Nim/issues/12381 - HttpClient socket
handle leak

To replicate the bug, run the following code in a loop:

```nim
import httpclient
while true:
    echo "New loop"
    var client = newHttpClient(timeout = 1000)
    try:
        let response = client.request("http://10.44.0.4/bla", httpMethod = HttpPost, body = "boo")
        echo "HTTP " & $response.status
    except CatchableError as e:
        echo "Error sending logs: " & $e.msg
    finally:
        echo "Finally"
        client.close()
```

Note the IP address as the hostname. I'm directly connecting to a
plausible local IP, but one that does not resolve, as I have everything
under 10.4.x.x.

The output looks like this to me:

```
New loop
Error sending logs: Operation timed out
Finally
New loop
Error sending logs: Operation timed out
Finally
New loop
...
```

In Nim 2.0.4, running the code above leaks the socket:

<img width="944" alt="Screenshot 2024-05-05 at 22 00 13"
src="https://github.com/nim-lang/Nim/assets/53387/ddac67db-d7df-45e6-b7a5-3d42f79775ea">

## Fix

With the added line of code, each old socket is cleanly removed:

<img width="938" alt="Screenshot 2024-05-05 at 21 54 18"
src="https://github.com/nim-lang/Nim/assets/53387/5b0b4b2d-d4f0-4e74-a9cf-74aec0c50d2e">

I believe the line below, `closeUnusedFds(ord(domain))` was supposed to
clean up the failed connection attempts, but it failed to do so for the
last one, assuming it succeeded. Yet it didn't. This fix makes sure
failed connections are closed immediately.

## Tests 

I don't have a test with this PR. When testing locally, the
`connect(lastFd, ..)` call on line 2032 blocks for ~75 seconds, ignoring
the http timeout. I fear any test I could add would either 1) take way
too long, 2) one day run in an environment where my randomly chosen IP
is real, yielding in weird flakes.

The only bug i can imagine is if running `lastFd.close()` twice is a bad
idea. I tested by actually running it twice, and... no crash/op? So
seems safe? I'm hoping the CI run will be green, and this will be
enough. However I'm happy to take feedback on how I should test this,
and do the necessary changes.

~Edit: looks like a test does fail, so moving to a draft while I figure
this out.~ Attempt 2 fixed it.
2024-05-08 09:33:43 -06:00
lit
6cc783f7f3 fixes #23442, fix for FileId under Windows (#23444)
See according issue:

Details:
<https://github.com/nim-lang/Nim/issues/23442#issuecomment-2021763669>

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-05-08 09:07:32 -06:00
lit
d772186b2d Update unicode.nim: cmpRunesIgnoreCase: fix doc format (#23560)
Its doc used to render wrongly where `>` is considered as quote block:


![image](https://github.com/nim-lang/Nim/assets/97860435/4aeda257-3231-42a5-9dd9-0052950a160e)
2024-05-02 16:07:19 +08:00
bptato
30cf570af9 Fix std/base64.decode out of bounds read (#23526)
inputLen may end up as 0 in the loop if the input string only includes
trailing characters. e.g. without the patch, decode(" ") would panic.
2024-04-22 09:44:33 +02:00
Pouriya Jamshidi
1bd0955218 fix JSON deep copy description (#23495)
Hi,

This is a tiny change, fixing the error in the documentation of JSON's
deep copy proc.
2024-04-12 14:14:33 +08:00
ringabout
9b378296f6 fixes addr/hiddenAddr in strictdefs (#23477) 2024-04-10 14:41:16 +02:00
lit
c23d6a3cb9 Update encodings.nim, fix open with bad arg raising no EncodingError (#23481)
On POSIX, `std/encodings` uses iconv, and `iconv_open` returns
`(iconv_t) -1` on failure, not `NULL`
2024-04-06 14:21:55 +02:00
ringabout
9e1b170a09 fixes #16771; lower swap for JS backend (#23473)
fixes #16771

follow up https://github.com/nim-lang/Nim/pull/16536

Ideally it should be handled in the IR part in the future

I have also checked the double evaluation of `swap` in the JS runtime
https://github.com/nim-lang/Nim/issues/16779, that might be solved by a
copy flag or something. Well, it should be best solved in the IR so that
it doesn't bother backends anymore.
2024-04-03 16:59:35 +02:00
Gianmarco
afc30a3b93 Fix compile time errors when using tables on 8/16-bits systems. (#23450)
Refer to the discussion in #23439.
2024-03-28 10:54:29 +01:00
Gianmarco
4c38569229 Change unicode lookup tables to have int32 elements to support platforms where sizeof(int) < 4 (#23433)
Fixes an issue that comes up when using strutils.`%` or any other
strutils/strformat feature that uses the unicode lookup tables behind
the scenes, on systems where ints are than 32-bit wide.

Tested with:

```bash
./koch test cat lib
```

Refer to the discussion in #23125.
2024-03-25 10:59:48 +01:00
Andreas Rumpf
6c4c60eade Adds support for custom ASTs in the Nim parser (#23417) 2024-03-18 20:27:00 +01:00
握猫猫
51837e8127 Fix #23381, Use sink and lent to avoid Future[object] making a copy (#23389)
fix #23381

As for the read function, the original plan was to use lent for
annotation, but after my experiments, it still produced copies, so I had
to move it out.

Now the `read` function cannot be called repeatedly
2024-03-14 11:24:39 +01:00
ringabout
248850a0ce ref #23333; fixes AF_INET6 value on Linux (#23334)
ref #23333
2024-03-03 17:52:56 +01:00
litlighilit
79bd6fe084 Update browsers.nim, deprecate unimplemented openDefaultBrowser() (#23332)
For this
[proc](773c066634/lib/pure/browsers.nim (L83))
`proc openDefaultBrowser*() {.since: (1, 1).}`:

though it's documented to open default browser with `about:blank` page,
it behaves differently:

- On Windows, it failed and open no window
- On Linux(Debian with Kde), it opens not default browser but
`Konqueror`

I have paid much effort to implement this variant, but even the
implementation on Windows is considerably complex.

In short, it's not only hard but unworthy to fix this.

Just as Araq
[said](https://github.com/nim-lang/Nim/issues/22250#issuecomment-1631360617),

we shall remove the `proc openDefaultBrowser*() {.since: (1, 1).}`
variant

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2024-03-03 17:27:27 +01:00
Jacek Sieka
a1e41930f8 strformat: detect format string errors at compile-time (#23356)
This also prevents unwanted `raises: [ValueError]` effects from bubbling
up from correct format strings which makes `fmt` broadly unusable with
`raises`.

The old runtime-based `formatValue` overloads are kept for
backwards-compatibility, should anyone be using runtime format strings.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-03-03 15:40:53 +01:00
Tomohiro
d6f0f1aca7 Remove count field from Deque (#23318)
This PR removes `count` field from `Deque` and get `count` from `tail -
head`.
2024-02-20 07:31:09 +01:00
ringabout
dfd778d056 fixes #23304; uses snprintf instead of sprintf (#23322)
fixes #23304
2024-02-20 07:28:45 +01:00
Tomohiro
ce68e11641 Remove mask field from Deque (#23299)
It seems Deque doesn't need `mask` field because `data.len - 1` equals
to `mask`.
Deque without `mask` field passes test `tests/stdlib/tdeques.nim`.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-02-11 00:39:32 +01:00
Antonis Geralis
c234a2a661 Add items and contains to heapqueue (#23296)
The implementation of these functions are trivial yet they were missing
from the module.
2024-02-09 13:19:36 +01:00
Tomohiro
befb383ac8 fixes #23275; Add == for Deque (#23276) 2024-02-08 22:18:52 +01:00
ringabout
a1d820367f follow up #22380; fixes incorrect usages of newWideCString (#23278)
follow up #22380
2024-02-05 12:14:21 +01:00
litlighilit
dd753b3383 Docs-improve: os.getCurrentCompilerExe replace with clearer short-desc (#23270)
The doc for `getCurrentCompilerExe` was originally added at [this
commit](c4e3c4ca2d),
saying "`getAppFilename` at CT", and modified as "This is
`getAppFilename()`_ at compile time..." since
[this](0c2c2dca2a (diff-8ed10106605d9e0e3f28a927432acd8312e96791c96dbb126a52a7010cf4b44a))

Which means "at compile time, get result innerly from Nim compiler via
`getAppFilename`", not "get from nim programs".

Thus, the doc was confusing, only mentioning `compile time` and
`getAppFilename`

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2024-02-02 20:11:49 +08:00
Angel Ezquerra
857b35c602 Additional speed ups of complex.pow (#23264)
This PR speeds up complex.pow when both base and exponent are real; when
only the exponent is real; and when the base is Euler's number. These
are some pretty common cases which appear in many formulas. The speed
ups are pretty significant. According to my measurements (using the
timeit library) when both base and exponent are real the speedup is ~2x;
when only the exponent is real it is ~1.5x and when the base is Euler's
number it is ~2x.

There is no measurable difference when using other exponents which makes
sense since I refactored the code a little to reduce the total number of
branches that are needed to get to the final "fallback" branch, and
those branches have less comparisons. Anecdotally the fallback case
feels slightly faster, but the improvement is so small that I cannot
claim an improvement. If it is there it is perhaps in the order of 3 or
4%.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-01-29 09:05:05 +01:00
ringabout
d3f5056bde remove unreachable code (#23244) 2024-01-22 16:47:21 +08:00
ringabout
301822e189 fixes a broken link in std/algorithm (#23246)
https://nim-lang.github.io/Nim/manual.html#procedures-do-notation
2024-01-22 16:45:56 +08:00
Angel Ezquerra
83f2708909 Speed up complex.pow when the exponent is 2.0 or 0.5 (#23237)
This PR speeds up the calculation of the power of a complex number when
the exponent is 2.0 or 0.5 (i.e the square and the square root of a
complex number). These are probably two of (if not) the most common
exponents. The speed up that is achieved according to my measurements
(using the timeit library) when the exponent is set to 2.0 or 0.5 is >
x7, while there is no measurable difference when using other exponents.

For the record, this is the function I used to mesure the performance:

```nim
import std/complex
import timeit

proc calculcatePows(v: seq[Complex], factor: Complex): seq[Complex] {.noinit, discardable.} =
  result = newSeq[Complex](v.len)
  for n in 0 ..< v.len:
    result[n] = pow(v[n], factor)

let v: seq[Complex64] = collect:
  for n in 0 ..< 1000:
    complex(float(n))

echo timeGo(calculcatePows(v, complex(1.5)))
echo timeGo(calculcatePows(v, complex(0.5)))
echo timeGo(calculcatePows(v, complex(2.0)))
```

Which with the original code got:

> [177μs 857.03ns] ± [1μs 234.85ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)
> [128μs 217.92ns] ± [1μs 630.93ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)
> [136μs 220.16ns] ± [3μs 475.56ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)

While with the improved code got:

> [176μs 884.30ns] ± [1μs 307.30ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)
> [23μs 160.79ns] ± [340.18ns] per loop (mean ± std. dev. of 7 runs,
10000 loops each)
> [19μs 93.29ns] ± [1μs 128.92ns] per loop (mean ± std. dev. of 7 runs,
10000 loops each)

That is, the new optimized path is 5.6 (23 vs 128 us per loop) to 7.16
times faster (19 vs 136 us per loop), while the non-optimized path takes
the same time as the original code.
2024-01-20 06:39:49 +01:00
Bung
01097fc1fc fix mime types data (#23226)
generated via https://github.com/bung87/mimetypes_gen

source data:
http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co
2024-01-19 13:11:01 +01:00
Angel Ezquerra
38f9ee0e58 Make std/math classify work without --passc:-fast-math. (#23211)
By using the existing isNaN function we can make std/math's classify
function work even if `--passc:-fast-math` is used.
2024-01-18 21:59:16 +01:00
Angel Ezquerra
2425f4559c Add ^ operator support for Rational numbers (#23219)
Since pow() cannot be supported for rationals, we support negative
integer exponents instead.
2024-01-18 14:32:22 +01:00
Zoom
8d1c722d2d Docs:strutils. Expand multiReplace docs, add runnableExamples (#23181)
- Clarified the implications of order of operation.
- Mentioned overlapping isn't handled
- Added the runnableExamples block

Fixes #23160, which supposedly should have been fixed in an earlier PR
#23022, but the wording was still not clear enough to my liking, which
the raised issue kind of confirms.
2024-01-08 08:23:24 +01:00
ASVIEST
20d79c9fb0 Deprecate asm stmt for js target (#23149)
why ?

- We already have an emit that does the same thing
- The name asm itself is a bit confusing, you might think it's an alias
for asm.js or something else.
- The asm keyword is used differently on different compiler targets (it
makes it inexpressive).
- Does anyone (other than some compiler libraries) use asm instead of
emit ? If yes, it's a bit strange to use asm somewhere and emit
somewhere. By making the asm keyword for js target deprecated, there
would be even less use of the asm keyword for js target, reducing the
amount of confusion.
- New users might accidentally use a non-universal approach via the asm
keyword instead of emit, and then when they learn about asm, try to
figure out what the differences are.

see https://forum.nim-lang.org/t/10821

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-01-02 07:49:54 +01:00