feat(users/sterni/nix): add sternis nix lib
What you see here is mostly the fallout of me implementing a correct urlencode implementation in nix for Profpatsch's blog implementation (although they'll probably keep it at arm's length). Where I want to go from here: * Extend this library towards general purpose nix™, mainly by implementing missing interfaces which you'd still have to use <nixpkgs/lib> for right now. Reexposing parts of <nixpkgs/lib> with better naming is fine for now, at some point I'd contemplate making this depend on nothing outside of depot, maybe even itself (should be easy we only use yants for an easily replaceable check). * Improve error messages possibly by carefully reintroducing yants. I originally typed essentially everything using yants, but turns out this can a) be dangerous when stuff you are handling throws because type checking means evaluating and b) has a incredible performance cost in some cases. * Reexpose builtins with better naming and slightly wrapped so they don't unrecoverably throw in cases where a null or something would suffice. Change-Id: I33ab08ca4e62dbc16b86c66c653935686e6b0e79 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2541 Reviewed-by: sterni <sternenseemann@systemli.org> Reviewed-by: Profpatsch <mail@profpatsch.de> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									369f504250
								
							
						
					
					
						commit
						3b33c1bd76
					
				
					 12 changed files with 811 additions and 0 deletions
				
			
		
							
								
								
									
										71
									
								
								users/sterni/nix/string/default.nix
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								users/sterni/nix/string/default.nix
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,71 @@ | |||
| { depot, lib, ... }: | ||||
| 
 | ||||
| let | ||||
| 
 | ||||
|   inherit (depot.users.sterni.nix.char) | ||||
|     chr | ||||
|     ord | ||||
|     ; | ||||
| 
 | ||||
|   inherit (depot.users.sterni.nix.flow) | ||||
|     cond | ||||
|     ; | ||||
| 
 | ||||
|   take = n: s: | ||||
|     builtins.substring 0 n s; | ||||
| 
 | ||||
|   drop = n: s: | ||||
|     builtins.substring n (builtins.stringLength s - n) s; | ||||
| 
 | ||||
|   charAt = i: s: | ||||
|     let | ||||
|       r = builtins.substring i 1 s; | ||||
|     in if r == "" then null else r; | ||||
| 
 | ||||
|   charIndex = char: s: | ||||
|     let | ||||
|       len = builtins.stringLength s; | ||||
|       go = i: | ||||
|         cond [ | ||||
|           [ (i >= len) null ] | ||||
|           [ (charAt i s == char) i ] | ||||
|           [ true (go (i + 1)) ] | ||||
|         ]; | ||||
|     in go 0; | ||||
| 
 | ||||
|   toChars = lib.stringToCharacters; | ||||
|   fromChars = lib.concatStrings; | ||||
| 
 | ||||
|   toBytes = str: | ||||
|     builtins.map ord (toChars str); | ||||
| 
 | ||||
|   fromBytes = is: lib.concatMapStrings chr is; | ||||
| 
 | ||||
|   pad = { left ? 0, right ? 0, char ? " " }: s: | ||||
|     let | ||||
|       leftS = fromChars (builtins.genList (_: char) left); | ||||
|       rightS = fromChars (builtins.genList (_: char) right); | ||||
|     in "${leftS}${s}${rightS}"; | ||||
| 
 | ||||
|   fit = { char ? " ", width, side ? "left" }: s: | ||||
|     let | ||||
|       diff = width - builtins.stringLength s; | ||||
|     in | ||||
|       if diff <= 0 | ||||
|       then s | ||||
|       else pad { inherit char; "${side}" = diff; } s; | ||||
| 
 | ||||
| in { | ||||
|   inherit | ||||
|     take | ||||
|     drop | ||||
|     charAt | ||||
|     charIndex | ||||
|     toBytes | ||||
|     fromBytes | ||||
|     toChars | ||||
|     fromChars | ||||
|     pad | ||||
|     fit | ||||
|     ; | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue