Commit Graph

210 Commits

Author SHA1 Message Date
Eman Resu
8dc96e955c lib.strings.fileContents: partially apply removeSuffix 2026-07-20 10:30:12 -04:00
Eman Resu
9a3a330251 lib.strings.hasInfix: partially apply match for infix 2026-07-20 10:28:42 -04:00
Eman Resu
131d2ecd4a lib.strings.hasPrefix: partially apply substring calls 2026-07-20 10:28:42 -04:00
Eman Resu
ed29e1b031 lib.strings: remove throwIfNot usage
throwIfNot sends our error message through a function call, even if the
error condition doesn't trigger. This requires a lot of thunk allocation
that can be easily avoided.
2026-06-30 21:38:27 -04:00
Eman Resu
855f2d0435 lib.strings: remove assertMsg usage
assertMsg sends our error message through a function call, even if the
error condition doesn't trigger. This requires a lot of thunk allocation
that can be easily avoided.
2026-06-30 21:38:24 -04:00
Eman Resu
ff92c9d15a lib.strings.cmakeFeature: beta reduce 2026-06-24 17:16:57 -04:00
Eman Resu
4755ddd126 lib.strings.{toSentenceCase,toCamelCase}: don't add context unnecessarily
builtins.substring doesn't destroy context - it's not necessary to add
it back.
2026-06-24 17:16:57 -04:00
Eman Resu
f9c9f70a40 lib.strings.cmakeBool: inline two function calls
I can live with a function like boolToString being used, but if we're
immediately modifying its output, we should homeroll it.
2026-06-24 17:16:56 -04:00
Eman Resu
184d899ccf lib.strings: remove duplicate cmake and meson assertions
These are already performed by cmakeOptionType and mesonOption - doing
them twice isn't necessary.
2026-06-24 17:16:56 -04:00
Eman Resu
a709ca8240 lib.strings.splitString: compute escaped separator early
Thanks to moving the existing let variable out, this should have
identical performance for non-memoised calls, and improved performance
when caching the separator.
2026-06-24 17:16:56 -04:00
Eman Resu
4a438d1a8a lib.strings.toSentenceCase: use -1 trick instead of computing stringLength 2026-05-15 23:45:25 -04:00
Eman Resu
f908b7a78c lib.strings.versionAtLeast: avoid an extra function call 2026-05-15 23:45:24 -04:00
Eman Resu
9ff4fe0e93 lib.strings.splitString: use builtins from global scope
These are all already inherited.
2026-05-15 23:45:15 -04:00
Eman Resu
7a0811c24b lib.strings.hasInfix: compute escaped infix early 2026-05-15 23:44:49 -04:00
Eman Resu
28a5dc3806 lib.strings.normalizePath: use our hasSuffix optimization 2026-05-15 23:44:48 -04:00
Eman Resu
35ba022673 lib.strings: compute prefix and suffix lengths early
Meaningful if these functions are partially applied. Saves 16% primops,
12% thunks, and 6% memory bytes when recursively importing all the nix
files in nixpkgs. Slightly increases memory usage when partial function
application isn't done, but I think the benefits outweigh the losses
here.
2026-05-15 23:17:03 -04:00
Eman Resu
94148625b0 lib.strings: check if throw is necessary before taking str
If these functions are called with partial function application (like
`lib.hasPrefix ".nix"`), then we can avoid performing the isPath check
multiple times, and only confirm it once. This gives 12% less primops
when recursively importing all nix files in nixpkgs.
2026-05-15 23:12:54 -04:00
Eman Resu
9f5c9ea4a8 lib.strings: throw instead of warning when passed a path
We've had this warning for years at this point. Doing this will also
give us a free performance win in the next commits.
2026-05-15 23:12:50 -04:00
Eman Resu
de312c12a2 lib.strings.toShellVar: only check validity of name once 2026-05-15 23:11:46 -04:00
Eman Resu
f765e4f33e lib.strings.splitStringBy: bring map into the inner loop 2026-05-15 23:11:45 -04:00
Eman Resu
00f9c30eaa lib.strings.makeSearchPathOutput: only map once 2026-05-15 23:11:45 -04:00
Eman Resu
25604d3e68 lib.strings.makeSearchPath: use concatMap rather than map and filter 2026-05-15 23:11:45 -04:00
Matt Sturgeon
986cb78f05 Reapply "lib/strings.concatLines: call concatStringsSep directly"
But also handle empty lists.

This effectively reapplies commit d7e6988c9b
which was reverted by commit 66f4e5d988.

Co-authored-by: Eman Resu <78693624+quatquatt@users.noreply.github.com>
2026-04-28 20:19:04 +01:00
Johannes Kirschbauer
66f4e5d988 Revert "lib/strings.concatLines: call concatStringsSep directly"
This reverts commit d7e6988c9b.
2026-04-28 10:04:39 +02:00
Johannes Kirschbauer
51f38b40cd lib/strings.concatLines: call concatStringsSep directly (#506077) 2026-04-16 19:32:28 +00:00
Eman Resu
d7e6988c9b lib/strings.concatLines: call concatStringsSep directly 2026-04-03 12:54:06 -04:00
İlkecan Bozdoğan
e394a579b0 lib: update type signatures
- concrete types start with uppercase: Int, String, Bool, Derivation,
  etc.
- type variables start with lowercase: a, b, etc.
- list:
  - use `[x]` for homogeneous lists instead of `List x` or `[ x ]`
  - use `List` for heterogeneous lists (not that common in `lib`)
- attr:
  - use `AttrSet` for a generic attribute set type
  - use `{ key1 :: Type1; key2 :: Type2; ... }` for adding signatures
    for known attribute names and types
  - use `{ key1 = value1; key2 = value2; ... }` for adding attributes
    with known literals
  - end with an ellipsis (`...`) if the set can contain unknown
    attributes
  - use `{ [String] :: x }` if all the attributes has the same type `x`
- prefer `Any` over `a` if the latter is not reused
2026-03-04 00:10:00 +03:00
Niclas Overby
7b16ea8b4e lib: re-export missing pure builtins in lib 2026-01-24 11:10:58 +01:00
Mikael Voss
f9fbbe20a5 lib/strings: order documented cmakeOptionType inputs correctly
The function is defined to take type, feature and value, but the
documented order was feature, type, value.
2026-01-14 20:29:20 -05:00
Shelvacu
5018a33b62 lib/strings.escapeNixIdentifier: quote keywords
Previously `lib.strings.escapeNixIdentifier "assert"` would return `"assert"`; This is wrong, `assert` is not a valid identifier unless quoted.\

This also fixes `lib.generators.toPretty`: `toPretty {} { "assert" = false; }` would previously return an invalid expression.
2026-01-02 13:47:32 -08:00
Anton Mosich
23808c75a8 docs/lib: Make links to clickable hyperlinks 2025-11-29 22:14:16 +01:00
Anton Mosich
31692961e7 docs/lib/strings: wrap command line arguments in quotes and backticks 2025-11-29 21:49:38 +01:00
Anton Mosich
367d102c09 docs/lib: use backticks for nix names 2025-11-29 21:46:54 +01:00
Jacob Abel
c137fe9433 lib/strings.nix: Fix cmakeOptionType argument order in docs
Arguments were listed out of order in the documentation. Fixes that.

Signed-off-by: Jacob Abel <jacobabel@nullpo.dev>
2025-11-28 20:17:12 -05:00
Aliaksandr
dc7095a86e treewide: remove deprecated lib functions that had warning for more than 2 years 2025-10-24 06:30:16 +03:00
Johannes Kirschbauer
bb9da33c62 lib/*: fix docs to use "returns" instead of "return" (#442388) 2025-10-06 08:09:54 +00:00
Robert Hensing
6a5f6f1a4e lib: init strings.join (#446278) 2025-10-02 15:34:34 +00:00
NAHO
6177c4ad72 lib: remove optional builtins prefixes from prelude functions
Remove optional builtins prefixes from prelude functions by running:

    builtins=(
      abort
      baseNameOf
      break
      derivation
      derivationStrict
      dirOf
      false
      fetchGit
      fetchMercurial
      fetchTarball
      fetchTree
      fromTOML
      import
      isNull
      map
      null
      placeholder
      removeAttrs
      scopedImport
      throw
      toString
      true
    )

    fd --type file . lib --exec-batch sed --in-place --regexp-extended "
      s/\<builtins\.($(
        printf '%s\n' "${builtins[@]}" |
          paste --delimiter '|' --serial -
      ))\>/\1/g
    "

    nix fmt
2025-09-30 09:02:59 +02:00
Johannes Kirschbauer
b37ac6a920 lib: init strings.join 2025-09-27 09:18:37 +02:00
Felix Buehler
06ac3fbebd lib/*: fix docs to use "returns" instead of "return" 2025-09-12 16:16:41 +02:00
Gutyina Gergő
b3b0fd266b lib: prefer replaceString over replaceStrings 2025-05-28 00:20:57 +02:00
Gutyina Gergő
b5720e2b3f lib/strings: init replaceString 2025-05-28 00:20:56 +02:00
Heitor Augusto
24e8a790b7 lib.strings: init toCamelCase 2025-05-11 20:24:56 -03:00
Pol Dellaiera
a9320986b3 lib.strings: init splitStringBy (#385643) 2025-04-23 04:51:38 +00:00
Mikael Voss
487a002c63 lib/strings: Zero‐pad hex digits in escapeC
lib.strings.escapeC produces single‐digit hexadecimal strings for
character values ≤ 15, which results in an ambiguity. If the following
character is a hex digit, it will be interpreted as being part of the
escape sequence.

systemd, which also relies on C‐style escape sequences, does not
decode single‐digit sequences at all, even if unambiguous.

Padding the hexadecimal string with "0" avoids this problem.
2025-04-18 13:41:53 +02:00
Heitor Augusto
bb4be9a474 lib.strings: init splitStringBy 2025-04-07 15:51:18 -03:00
Silvan Mosberger
e52d633a63 Merge remote-tracking branch 'upstream/staging-next' into staging 2025-04-02 18:30:54 +02:00
Silvan Mosberger
374e6bcc40 treewide: Format all Nix files
Format all Nix files using the officially approved formatter,
making the CI check introduced in the previous commit succeed:

  nix-build ci -A fmt.check

This is the next step of the of the [implementation](https://github.com/NixOS/nixfmt/issues/153)
of the accepted [RFC 166](https://github.com/NixOS/rfcs/pull/166).

This commit will lead to merge conflicts for a number of PRs,
up to an estimated ~1100 (~33%) among the PRs with activity in the past 2
months, but that should be lower than what it would be without the previous
[partial treewide format](https://github.com/NixOS/nixpkgs/pull/322537).

Merge conflicts caused by this commit can now automatically be resolved while rebasing using the
[auto-rebase script](8616af08d9/maintainers/scripts/auto-rebase).

If you run into any problems regarding any of this, please reach out to the
[formatting team](https://nixos.org/community/teams/formatting/) by
pinging @NixOS/nix-formatting.
2025-04-01 20:10:43 +02:00
Tristan Ross
b26e122b14 lib.cmakeOptionType: support list 2025-03-26 21:12:07 -07:00
Mikael Voss
7a07cc0da9 lib/strings: allow CA paths in isStorePath 2025-03-05 13:08:41 +01:00