Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)

lib/strings: Update documentation comments for doc generation

Updates documentation comments with extra information for nixdoc[1]
compatibility.

Some documentation strings have additionally been reworded for
clarity.

"Faux types" are added where applicable, but some functions do things
that are not trivially representable in the type notation used so they
were ignored for this purpose.

[1]: https://github.com/tazjin/nixdoc

+153 -51
+153 -51
lib/strings.nix
··· 12 12 13 13 /* Concatenate a list of strings. 14 14 15 + Type: concatStrings :: [string] -> string 16 + 15 17 Example: 16 18 concatStrings ["foo" "bar"] 17 19 => "foobar" ··· 19 21 concatStrings = builtins.concatStringsSep ""; 20 22 21 23 /* Map a function over a list and concatenate the resulting strings. 24 + 25 + Type: concatMapStrings :: (a -> string) -> [a] -> string 22 26 23 27 Example: 24 28 concatMapStrings (x: "a" + x) ["foo" "bar"] ··· 26 30 */ 27 31 concatMapStrings = f: list: concatStrings (map f list); 28 32 29 - /* Like `concatMapStrings' except that the f functions also gets the 33 + /* Like `concatMapStrings` except that the f functions also gets the 30 34 position as a parameter. 31 35 36 + Type: concatImapStrings :: (int -> a -> string) -> [a] -> string 37 + 32 38 Example: 33 39 concatImapStrings (pos: x: "${toString pos}-${x}") ["foo" "bar"] 34 40 => "1-foo2-bar" ··· 36 42 concatImapStrings = f: list: concatStrings (lib.imap1 f list); 37 43 38 44 /* Place an element between each element of a list 45 + 46 + Type: intersperse :: a -> [a] -> [a] 39 47 40 48 Example: 41 49 intersperse "/" ["usr" "local" "bin"] 42 50 => ["usr" "/" "local" "/" "bin"]. 43 51 */ 44 - intersperse = separator: list: 52 + intersperse = 53 + # Separator to add between elements 54 + separator: 55 + # Input list 56 + list: 45 57 if list == [] || length list == 1 46 58 then list 47 59 else tail (lib.concatMap (x: [separator x]) list); 48 60 49 61 /* Concatenate a list of strings with a separator between each element 50 62 63 + Type: concatStringsSep :: string -> [string] -> string 64 + 51 65 Example: 52 66 concatStringsSep "/" ["usr" "local" "bin"] 53 67 => "usr/local/bin" ··· 55 69 concatStringsSep = builtins.concatStringsSep or (separator: list: 56 70 concatStrings (intersperse separator list)); 57 71 58 - /* First maps over the list and then concatenates it. 72 + /* Maps a function over a list of strings and then concatenates the 73 + result with the specified separator interspersed between 74 + elements. 75 + 76 + Type: concatMapStringsSep :: string -> (string -> string) -> [string] -> string 59 77 60 78 Example: 61 79 concatMapStringsSep "-" (x: toUpper x) ["foo" "bar" "baz"] 62 80 => "FOO-BAR-BAZ" 63 81 */ 64 - concatMapStringsSep = sep: f: list: concatStringsSep sep (map f list); 82 + concatMapStringsSep = 83 + # Separator to add between elements 84 + sep: 85 + # Function to map over the list 86 + f: 87 + # List of input strings 88 + list: concatStringsSep sep (map f list); 89 + 90 + /* Same as `concatMapStringsSep`, but the mapping function 91 + additionally receives the position of its argument. 65 92 66 - /* First imaps over the list and then concatenates it. 93 + Type: concatMapStringsSep :: string -> (int -> string -> string) -> [string] -> string 67 94 68 95 Example: 69 - 70 96 concatImapStringsSep "-" (pos: x: toString (x / pos)) [ 6 6 6 ] 71 97 => "6-3-2" 72 98 */ 73 - concatImapStringsSep = sep: f: list: concatStringsSep sep (lib.imap1 f list); 99 + concatImapStringsSep = 100 + # Separator to add between elements 101 + sep: 102 + # Function that receives elements and their positions 103 + f: 104 + # List of input strings 105 + list: concatStringsSep sep (lib.imap1 f list); 74 106 75 - /* Construct a Unix-style search path consisting of each `subDir" 76 - directory of the given list of packages. 107 + /* Construct a Unix-style, colon-separated search path consisting of 108 + the given `subDir` appended to each of the given paths. 109 + 110 + Type: makeSearchPath :: string -> [string] -> string 77 111 78 112 Example: 79 113 makeSearchPath "bin" ["/root" "/usr" "/usr/local"] 80 114 => "/root/bin:/usr/bin:/usr/local/bin" 81 - makeSearchPath "bin" ["/"] 82 - => "//bin" 115 + makeSearchPath "bin" [""] 116 + => "/bin" 83 117 */ 84 - makeSearchPath = subDir: packages: 85 - concatStringsSep ":" (map (path: path + "/" + subDir) (builtins.filter (x: x != null) packages)); 118 + makeSearchPath = 119 + # Directory name to append 120 + subDir: 121 + # List of base paths 122 + paths: 123 + concatStringsSep ":" (map (path: path + "/" + subDir) (builtins.filter (x: x != null) paths)); 86 124 87 - /* Construct a Unix-style search path, using given package output. 88 - If no output is found, fallback to `.out` and then to the default. 125 + /* Construct a Unix-style search path by appending the given 126 + `subDir` to the specified `output` of each of the packages. If no 127 + output by the given name is found, fallback to `.out` and then to 128 + the default. 129 + 130 + Type: string -> string -> [package] -> string 89 131 90 132 Example: 91 133 makeSearchPathOutput "dev" "bin" [ pkgs.openssl pkgs.zlib ] 92 134 => "/nix/store/9rz8gxhzf8sw4kf2j2f1grr49w8zx5vj-openssl-1.0.1r-dev/bin:/nix/store/wwh7mhwh269sfjkm6k5665b5kgp7jrk2-zlib-1.2.8/bin" 93 135 */ 94 - makeSearchPathOutput = output: subDir: pkgs: makeSearchPath subDir (map (lib.getOutput output) pkgs); 136 + makeSearchPathOutput = 137 + # Package output to use 138 + output: 139 + # Directory name to append 140 + subDir: 141 + # List of packages 142 + pkgs: makeSearchPath subDir (map (lib.getOutput output) pkgs); 95 143 96 144 /* Construct a library search path (such as RPATH) containing the 97 145 libraries for a set of packages ··· 117 165 118 166 /* Construct a perl search path (such as $PERL5LIB) 119 167 120 - FIXME(zimbatm): this should be moved in perl-specific code 121 - 122 168 Example: 123 169 pkgs = import <nixpkgs> { } 124 170 makePerlPath [ pkgs.perlPackages.libnet ] 125 171 => "/nix/store/n0m1fk9c960d8wlrs62sncnadygqqc6y-perl-Net-SMTP-1.25/lib/perl5/site_perl" 126 172 */ 173 + # FIXME(zimbatm): this should be moved in perl-specific code 127 174 makePerlPath = makeSearchPathOutput "lib" "lib/perl5/site_perl"; 128 175 129 176 /* Construct a perl search path recursively including all dependencies (such as $PERL5LIB) ··· 138 185 /* Depending on the boolean `cond', return either the given string 139 186 or the empty string. Useful to concatenate against a bigger string. 140 187 188 + Type: optionalString :: bool -> string -> string 189 + 141 190 Example: 142 191 optionalString true "some-string" 143 192 => "some-string" 144 193 optionalString false "some-string" 145 194 => "" 146 195 */ 147 - optionalString = cond: string: if cond then string else ""; 196 + optionalString = 197 + # Condition 198 + cond: 199 + # String to return if condition is true 200 + string: if cond then string else ""; 148 201 149 202 /* Determine whether a string has given prefix. 203 + 204 + Type: hasPrefix :: string -> string -> bool 150 205 151 206 Example: 152 207 hasPrefix "foo" "foobar" ··· 154 209 hasPrefix "foo" "barfoo" 155 210 => false 156 211 */ 157 - hasPrefix = pref: str: 158 - substring 0 (stringLength pref) str == pref; 212 + hasPrefix = 213 + # Prefix to check for 214 + pref: 215 + # Input string 216 + str: substring 0 (stringLength pref) str == pref; 159 217 160 218 /* Determine whether a string has given suffix. 219 + 220 + Type: hasSuffix :: string -> string -> bool 161 221 162 222 Example: 163 223 hasSuffix "foo" "foobar" ··· 165 225 hasSuffix "foo" "barfoo" 166 226 => true 167 227 */ 168 - hasSuffix = suffix: content: 228 + hasSuffix = 229 + # Suffix to check for 230 + suffix: 231 + # Input string 232 + content: 169 233 let 170 234 lenContent = stringLength content; 171 235 lenSuffix = stringLength suffix; ··· 180 244 Also note that Nix treats strings as a list of bytes and thus doesn't 181 245 handle unicode. 182 246 247 + Type: stringtoCharacters :: string -> [string] 248 + 183 249 Example: 184 250 stringToCharacters "" 185 251 => [ ] ··· 194 260 /* Manipulate a string character by character and replace them by 195 261 strings before concatenating the results. 196 262 263 + Type: stringAsChars :: (string -> string) -> string -> string 264 + 197 265 Example: 198 266 stringAsChars (x: if x == "a" then "i" else x) "nax" 199 267 => "nix" 200 268 */ 201 - stringAsChars = f: s: 202 - concatStrings ( 269 + stringAsChars = 270 + # Function to map over each individual character 271 + f: 272 + # Input string 273 + s: concatStrings ( 203 274 map f (stringToCharacters s) 204 275 ); 205 276 206 - /* Escape occurrence of the elements of ‘list’ in ‘string’ by 277 + /* Escape occurrence of the elements of `list` in `string` by 207 278 prefixing it with a backslash. 208 279 280 + Type: escape :: [string] -> string -> string 281 + 209 282 Example: 210 283 escape ["(" ")"] "(foo)" 211 284 => "\\(foo\\)" ··· 213 286 escape = list: replaceChars list (map (c: "\\${c}") list); 214 287 215 288 /* Quote string to be used safely within the Bourne shell. 289 + 290 + Type: escapeShellArg :: string -> string 216 291 217 292 Example: 218 293 escapeShellArg "esc'ape\nme" ··· 222 297 223 298 /* Quote all arguments to be safely passed to the Bourne shell. 224 299 300 + Type: escapeShellArgs :: [string] -> string 301 + 225 302 Example: 226 303 escapeShellArgs ["one" "two three" "four'five"] 227 304 => "'one' 'two three' 'four'\\''five'" ··· 230 307 231 308 /* Turn a string into a Nix expression representing that string 232 309 310 + Type: string -> string 311 + 233 312 Example: 234 313 escapeNixString "hello\${}\n" 235 314 => "\"hello\\\${}\\n\"" 236 315 */ 237 316 escapeNixString = s: escape ["$"] (builtins.toJSON s); 238 317 239 - /* Obsolete - use replaceStrings instead. */ 318 + # Obsolete - use replaceStrings instead. 240 319 replaceChars = builtins.replaceStrings or ( 241 320 del: new: s: 242 321 let ··· 256 335 257 336 /* Converts an ASCII string to lower-case. 258 337 338 + Type: toLower :: string -> string 339 + 259 340 Example: 260 341 toLower "HOME" 261 342 => "home" ··· 263 344 toLower = replaceChars upperChars lowerChars; 264 345 265 346 /* Converts an ASCII string to upper-case. 347 + 348 + Type: toUpper :: string -> string 266 349 267 350 Example: 268 351 toUpper "home" ··· 273 356 /* Appends string context from another string. This is an implementation 274 357 detail of Nix. 275 358 276 - Strings in Nix carry an invisible `context' which is a list of strings 359 + Strings in Nix carry an invisible `context` which is a list of strings 277 360 representing store paths. If the string is later used in a derivation 278 361 attribute, the derivation will properly populate the inputDrvs and 279 362 inputSrcs. ··· 319 402 in 320 403 recurse 0 0; 321 404 322 - /* Return the suffix of the second argument if the first argument matches 323 - its prefix. 405 + /* Return a string without the specified prefix, if the prefix matches. 406 + 407 + Type: string -> string -> string 324 408 325 409 Example: 326 410 removePrefix "foo." "foo.bar.baz" ··· 328 412 removePrefix "xxx" "foo.bar.baz" 329 413 => "foo.bar.baz" 330 414 */ 331 - removePrefix = pre: s: 415 + removePrefix = 416 + # Prefix to remove if it matches 417 + prefix: 418 + # Input string 419 + str: 332 420 let 333 - preLen = stringLength pre; 334 - sLen = stringLength s; 421 + preLen = stringLength prefix; 422 + sLen = stringLength str; 335 423 in 336 - if hasPrefix pre s then 337 - substring preLen (sLen - preLen) s 424 + if hasPrefix prefix str then 425 + substring preLen (sLen - preLen) str 338 426 else 339 - s; 427 + str; 428 + 429 + /* Return a string without the specified suffix, if the suffix matches. 340 430 341 - /* Return the prefix of the second argument if the first argument matches 342 - its suffix. 431 + Type: string -> string -> string 343 432 344 433 Example: 345 434 removeSuffix "front" "homefront" ··· 347 436 removeSuffix "xxx" "homefront" 348 437 => "homefront" 349 438 */ 350 - removeSuffix = suf: s: 439 + removeSuffix = 440 + # Suffix to remove if it matches 441 + suffix: 442 + # Input string 443 + str: 351 444 let 352 - sufLen = stringLength suf; 353 - sLen = stringLength s; 445 + sufLen = stringLength suffix; 446 + sLen = stringLength str; 354 447 in 355 - if sufLen <= sLen && suf == substring (sLen - sufLen) sufLen s then 356 - substring 0 (sLen - sufLen) s 448 + if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then 449 + substring 0 (sLen - sufLen) str 357 450 else 358 - s; 451 + str; 359 452 360 - /* Return true iff string v1 denotes a version older than v2. 453 + /* Return true if string v1 denotes a version older than v2. 361 454 362 455 Example: 363 456 versionOlder "1.1" "1.2" ··· 367 460 */ 368 461 versionOlder = v1: v2: builtins.compareVersions v2 v1 == 1; 369 462 370 - /* Return true iff string v1 denotes a version equal to or newer than v2. 463 + /* Return true if string v1 denotes a version equal to or newer than v2. 371 464 372 465 Example: 373 466 versionAtLeast "1.1" "1.0" ··· 459 552 /* Create a fixed width string with additional prefix to match 460 553 required width. 461 554 555 + This function will fail if the input string is longer than the 556 + requested length. 557 + 558 + Type: fixedWidthString :: int -> string -> string 559 + 462 560 Example: 463 561 fixedWidthString 5 "0" (toString 15) 464 562 => "00015" ··· 506 604 && builtins.substring 0 1 (toString x) == "/" 507 605 && dirOf x == builtins.storeDir; 508 606 509 - /* Convert string to int 510 - Obviously, it is a bit hacky to use fromJSON that way. 607 + /* Parse a string string as an int. 608 + 609 + Type: string -> int 511 610 512 611 Example: 513 612 toInt "1337" ··· 517 616 toInt "3.14" 518 617 => error: floating point JSON numbers are not supported 519 618 */ 619 + # Obviously, it is a bit hacky to use fromJSON this way. 520 620 toInt = str: 521 621 let may_be_int = builtins.fromJSON str; in 522 622 if builtins.isInt may_be_int 523 623 then may_be_int 524 624 else throw "Could not convert ${str} to int."; 525 625 526 - /* Read a list of paths from `file', relative to the `rootPath'. Lines 527 - beginning with `#' are treated as comments and ignored. Whitespace 528 - is significant. 626 + /* Read a list of paths from `file`, relative to the `rootPath`. 627 + Lines beginning with `#` are treated as comments and ignored. 628 + Whitespace is significant. 529 629 530 - NOTE: this function is not performant and should be avoided 630 + NOTE: This function is not performant and should be avoided. 531 631 532 632 Example: 533 633 readPathsFromFile /prefix ··· 548 648 absolutePaths; 549 649 550 650 /* Read the contents of a file removing the trailing \n 651 + 652 + Type: fileContents :: path -> string 551 653 552 654 Example: 553 655 $ echo "1.0" > ./version