Skip to content

Commit

Permalink
util: deduplicate stripStr implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adisbladis committed Oct 25, 2023
1 parent 5ae21e7 commit 29a5cbe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
5 changes: 1 addition & 4 deletions lib/pep508.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let
inherit (builtins) match elemAt split foldl' substring stringLength typeOf fromJSON isString head mapAttrs elem length;
inherit (lib) stringToCharacters fix;
inherit (import ./util.nix { inherit lib; }) splitComma;
inherit (import ./util.nix { inherit lib; }) splitComma stripStr;

re = {
operators = "([=><!~^]+)";
Expand Down Expand Up @@ -34,9 +34,6 @@ let
}
);

# Strip leading/trailing whitespace from string
stripStr = s: let t = match "[\t ]*(.*[^\t ])[\t ]*" s; in if t == null then "" else head t;

# Remove groupings ( ) from expression
unparen = expr':
let
Expand Down
9 changes: 1 addition & 8 deletions lib/pip.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
}:
let
inherit (builtins) match head tail typeOf split filter foldl' readFile dirOf hasContext unsafeDiscardStringContext;

stripStr = s:
let
t = match "[\t ]*(.*[^\t ])[\t ]*" s;
in
if t == null
then ""
else head t;
inherit (import ./util.nix { inherit lib; }) stripStr;

uncomment = l: head (match " *([^#]*).*" l);

Expand Down
10 changes: 9 additions & 1 deletion lib/util.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Small utilities for internal reuse, not exposed externally
{ lib }:
let
inherit (builtins) filter match split;
inherit (builtins) filter match split head;
inherit (lib) isString;

isEmptyStr = s: isString s && match " *" s == null;
in
{
splitComma = s: if s == "" then [ ] else filter isEmptyStr (split " *, *" s);

stripStr = s:
let
t = match "[\t ]*(.*[^\t ])[\t ]*" s;
in
if t == null
then ""
else head t;
}

0 comments on commit 29a5cbe

Please sign in to comment.