2023-01-09 03:24:10 +08:00
|
|
|
# Copyright (C) 2022-2023 Exaloop Inc. <https://exaloop.io>
|
2022-12-13 09:54:01 +08:00
|
|
|
|
|
|
|
def _format_error(ret: str):
|
|
|
|
raise ValueError(f"invalid format specifier: {ret}")
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class int:
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
|
|
err = False
|
|
|
|
ret = _C.seq_str_int(self, format_spec, __ptr__(err))
|
2023-01-06 00:40:49 +08:00
|
|
|
if format_spec and err:
|
2022-12-13 09:54:01 +08:00
|
|
|
_format_error(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class Int:
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
|
|
err = False
|
|
|
|
ret = _C.seq_str_int(self.__int__(), format_spec, __ptr__(err))
|
2023-01-06 00:40:49 +08:00
|
|
|
if format_spec and err:
|
2022-12-13 09:54:01 +08:00
|
|
|
_format_error(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class UInt:
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
|
|
err = False
|
|
|
|
ret = _C.seq_str_uint(self.__int__(), format_spec, __ptr__(err))
|
2023-01-06 00:40:49 +08:00
|
|
|
if format_spec and err:
|
2022-12-13 09:54:01 +08:00
|
|
|
_format_error(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class float:
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
|
|
err = False
|
|
|
|
ret = _C.seq_str_float(self, format_spec, __ptr__(err))
|
2023-01-06 00:40:49 +08:00
|
|
|
if format_spec and err:
|
2022-12-13 09:54:01 +08:00
|
|
|
_format_error(ret)
|
|
|
|
return ret if ret != "-nan" else "nan"
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class str:
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
|
|
err = False
|
|
|
|
ret = _C.seq_str_str(self, format_spec, __ptr__(err))
|
2023-01-06 00:40:49 +08:00
|
|
|
if format_spec and err:
|
2022-12-13 09:54:01 +08:00
|
|
|
_format_error(ret)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@extend
|
|
|
|
class Ptr:
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
|
|
err = False
|
|
|
|
ret = _C.seq_str_ptr(self.as_byte(), format_spec, __ptr__(err))
|
2023-01-06 00:40:49 +08:00
|
|
|
if format_spec and err:
|
2022-12-13 09:54:01 +08:00
|
|
|
_format_error(ret)
|
|
|
|
return ret
|