337 Commits

Author SHA1 Message Date
Andrew Kelley
6ab5219e34 std: update test cases to reflect new packed struct semantics 2022-07-12 18:38:11 -07:00
Andrew Kelley
cbdd21cd9a std: disable tests regressed from LLVM 14 2022-07-03 15:39:39 -07:00
Motiejus Jakštys
4a6b70fbd1
mem: add splitBackwards (#11908)
* mem: refactor tests of split()

- add a few cases for .rest()
- use expectEqualSlices()

* mem: add splitBackwards

Over the last couple of weeks weeks I needed to iterate over a
collection backwards at least twice. Do we want to have this in stdlib?
If yes, click "Merge" and start using today! Free shipping and returns
(before 1.0).

Why is this useful?
-------------------

I need this for building an error wrapper: errors are added in the
wrapper from "lowest" level to "highest" level, and then printed in
reverse order. Imagine `UpdateUsers` call, which needs to return
`error.InvalidInput` and a wrappable error context. In Go we would add a
context to the error when returning it:

    // if update_user fails, add context on which user we are operating
    if err := update_user(user); err != nil {
        return fmt.Errorf("user id=%d: %w", user.id, err)
    }

Since Zig cannot pass anything else than u16 with an error (#2647), I
will pass a `err_ctx: *Err`, to the callers, where they can, besides
returning an error, augment it with auxiliary data. `Err` is a
preallocated array that can add zero-byte-separated strings. For a
concrete example, imagine such a call graph:

    update_user(User, *Err) error{InvalidInput}!<...>
      validate_user([]const u8, *Err) error{InvalidInput}!<...>

Where `validate_user` would like, besides only the error, signal the
invalid field. And `update_user`, besides the error, would signal the
offending user id.

We also don't want the low-level functions to know in which context they
are operating to construct a meaningful error message: if validation
fails, they append their "context" to the buffer. To translate/augment
the Go example above:

    pub fn validate_user(err_ctx: *Err, user: User) error{InvalidInput}!void {
        const name = user.name;
        if (!ascii.isAlpha(name)) {
            err_ctx.print("name '{s}' must be ascii-letters only", .{name});
            return error.InvalidInput;
        }
        <...>
    }

    // update_user validates each user and does something with it.
    pub fn update_user(err_ctx: *Err, user: User) error{InvalidInput}!void {
        // validate the user before updating it
        validate_user(user) catch {
            err_ctx.print("user id={d}", .{user.id});
            return error.InvalidInput;
        };
        <...>
    }

Then the top-level function (in my case, CLI) will read the buffer
backwards (splitting on `"\x00"`) and print:

    user id=123: name 'Žvangalas' must be ascii-letters only

To read that buffer backwards, dear readers of this commit message, I
need `mem.splitBackwards`.
2022-06-29 08:23:09 +02:00
Ben Fiedler
76546b3f8e std.mem: add peek() to TokenIterator(T) 2022-06-28 21:45:02 +02:00
Veikka Tuominen
8f9b31af92 value: handle slices in canMutateComptimeVarState 2022-06-18 20:04:49 +03:00
Andrew Kelley
ffa700ee58
Merge pull request #11837 from Vexu/stage2
Fix (nearly) all stage2 crashes when testing stdlib
2022-06-12 17:45:57 -04:00
Andrew Kelley
85492f2b91 std.mem.zeroes: remove call to std.meta
everybody is so horny for std.meta
2022-06-12 00:56:59 -07:00
Veikka Tuominen
6b36774adc std: disable failing tests, add zig2 build test-std to CI 2022-06-12 10:43:28 +03:00
Koakuma
fb0692334e target: Rename sparcv9 -> sparc64
Rename all references of sparcv9 to sparc64, to make Zig align more with
other projects. Also, added new function to convert glibc arch name to Zig
arch name, since it refers to the architecture as sparcv9.

This is based on the suggestion by @kubkon in PR 11847.
(https://github.com/ziglang/zig/pull/11487#pullrequestreview-963761757)
2022-05-13 16:43:59 -04:00
Yusuf Bham
41654a318d std.mem: add concatWithSentinel 2022-04-21 14:39:41 -04:00
ominitay
25d4c5df70
std.mem.zeroInit: Fix behaviour with empty initialiser 2022-03-28 14:10:36 +03:00
Andrew Kelley
fd85cfe154 std.mem: remove redundant namespaces in test names
related: #7923
2022-03-09 15:44:08 -07:00
Jonathan Marler
d805adddd6 deprecated TypeInfo in favor of Type
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-03-08 20:38:12 +02:00
Evan Haas
e382e7be2b std: Allow mem.zeroes to work at comptime with extern union
Fixes #10797
2022-02-07 11:34:20 +02:00
Andrew Kelley
f4a249325e stage1: avoid anytype fields for type info
prerequisite for #10705
2022-02-01 18:10:19 -07:00
Veikka Tuominen
88edde4edc
Merge pull request #9915 from zzyxyzz/indexOfMinMax
std.mem: add indexOfMin and indexOfMax
2022-01-29 15:49:47 +02:00
John Schmidt
63ee6e6625 Rename mem.bswapAllFields to byteSwapAllFields
To match the renaming of `@bswap` to `@byteSwap` in
1fdb24827f.
2022-01-28 21:03:21 -05:00
Mikhail Popov
100b8a244c Add std.mem.minMax() and std.mem.IndexOfMinMax()
For finding the minimum and maximum values (and indices)
in a slice in a single pass.
2022-01-27 21:22:00 +02:00
Mikhail Popov
dddbd2f511 std.mem: Add functions to find the index of the mimimum/maximum value in a slice. 2022-01-27 19:58:27 +02:00
Josh Hannaford
178cd60a5e Update the documentation for std.mem.sliceTo for readability 2022-01-26 00:49:17 -05:00
Ali Chraghi
042b770d62
std: Skip comptime struct fields in mem.zeroes() (#10406)
closes #9934
2021-12-27 23:33:11 -05:00
Isaac Freund
9f9f215305
stage1, stage2: rename c_void to anyopaque (#10316)
zig fmt now replaces c_void with anyopaque to make updating
code easy.
2021-12-19 00:24:45 -05:00
Lee Cannon
066eaa5e9c
allocgate: change resize to return optional instead of error 2021-11-30 23:45:01 +00:00
Lee Cannon
f68cda738a
allocgate: split free out from resize 2021-11-30 23:32:48 +00:00
Lee Cannon
9377f32c08
allocgate: utilize a *const vtable field 2021-11-30 23:32:48 +00:00
Lee Cannon
80bbf234e0
allocgate: fix failing tests 2021-11-30 23:32:48 +00:00
Lee Cannon
1093b09a98
allocgate: renamed getAllocator function to allocator 2021-11-30 23:32:47 +00:00
Lee Cannon
47bc13bc59
allocgate: dont use a dummy temporary for stateless allocators 2021-11-30 23:32:47 +00:00
Lee Cannon
85de022c56
allocgate: std Allocator interface refactor 2021-11-30 23:32:47 +00:00
Andrew Kelley
902df103c6 std lib API deprecations for the upcoming 0.9.0 release
See #3811
2021-11-30 00:13:07 -07:00
Fabio Arnold
da7baf7dae
std.mem.indexOfPos should return start_index when needle length is zero (#10220)
Closes #10216
2021-11-25 20:56:38 -05:00
Trioct
b644d49365 Fix type error for u8 in writeIntSlice 2021-11-21 16:47:39 -05:00
Andrew Kelley
6115cf2240 migrate from std.Target.current to @import("builtin").target
closes #9388
closes #9321
2021-10-04 23:48:55 -07:00
Coleman Broaddus
e14fcd60cb
FIX resize() for non u8 element types. (#9806) 2021-09-22 12:09:16 +03:00
Robin Voetter
c5945467ac Address Spaces: Pointer and function info in @Type 2021-09-20 02:29:04 +02:00
jdmichaud
49c9975484
zig fmt: respect trailing commas in inline assembly 2021-08-29 11:57:32 +02:00
Andrew Kelley
d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00
Ryan Liptak
493822ac3b Update mem.split/mem.tokenize doc comments
Follow up to #9531
2021-08-11 21:19:03 -04:00
Ryan Liptak
05fd20dc10 Make mem.split and mem.tokenize generic instead of assuming u8
This allows these functions to work on slices of u16, etc
2021-08-06 01:53:07 -07:00
Andrew Kelley
d979dd9b58 stage2: improve AstGen FileNotFound error message
Partially addresses #9203. It fixes the first case, but not the second
one mentioned in the issue.
2021-07-02 15:27:00 -07:00
Tom Maenan Read Cutting
177b1b6bf9 Add fat/universal dylib support to zig ld
With this change zig ld can link with dynamic libraries
contained within a fat/universal file that had multiple
seperate binaries embedded within it for multi-arch
support (in macOS).

Whilst zig can still only create single-architecture
executables - the ability to link with fat libraries is
useful for cases where they are the easiest (or only)
option to link against.
2021-06-25 08:38:47 +02:00
Jacob G-W
9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Andrew Kelley
b973e0a3e3 fix doc comments copy paste typo 2021-06-06 18:05:06 -07:00
LemonBoy
0c091f8b09 std: Add helpers to safely align pointers
Add two helpers to ensure people won't ignore some edge cases such as
pointers overflowing the address space.

Also fix #8924 to some degree, the amount of unchecked alignForward is
still scary.
2021-06-06 18:04:16 -07:00
Kenta Iwasaki
4909aa1da4 os/bits: remove duplicate sockaddr_storage for dragonfly 2021-06-01 18:35:13 +09:00
Kenta Iwasaki
6950e4c294 x/os/net: remove unnecessary comptime prefix in resolveScopeID() 2021-06-01 18:24:43 +09:00
Matthew Borkowski
54f774f796 make writeIntSlice functions work for signed integers 2021-05-28 19:43:58 -04:00
Matthew Borkowski
d7bf4f8070 fix Boyer-Moore-Horspool algorithm in indexOfPos and lastIndexOf when element type is larger than a byte 2021-05-22 20:51:17 -04:00
Andrew Kelley
7cd9b30e0a
Merge pull request #7664 from marler8997/fixWindowsPaths
implement nt path conversion for windows
2021-05-22 17:58:59 -04:00
Matthew Borkowski
f4101c1153
fix accidental quadratic dependence on haystack length in replace and replacementSize (#8859) 2021-05-22 10:55:28 +02:00