bcrypt.ctf/bcrypter/lib/format.py

29 lines
665 B
Python
Raw Normal View History

2025-06-21 19:05:47 +10:00
from typing import Any
from bcrypt.lib.math import clamp_min
'''
Apply left padding to str(x) for parameter x: Any
'''
def lpad(x: Any, n: int, pad: chr = ' ') -> str:
x = str(x)
width = clamp_min(n - len(x), 0)
return width * pad + x
'''
Left pad an integer's binary representation with zeros
'''
def lpadbin(x: int, n: int) -> str:
return lpad(bin(x)[2:], n, pad='0')
'''
Extends the standard lstrip string method,
allowing the max: int kwarg to be supplied.
'''
def lstrip(s: str, prefix: str, max: int = -1) -> str:
l = len(prefix)
c = 0
while s.startswith(prefix) and c < max:
s = s[l:]
c += 1
return s