1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/stdlib/internal/format.codon
A. R. Shajii 0bec6e2627
Refactor IR; updates for 2023 (#157)
* Refactor IR

* Update image size in docs
2023-01-08 14:24:10 -05:00

59 lines
1.6 KiB
Python

# Copyright (C) 2022-2023 Exaloop Inc. <https://exaloop.io>
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))
if format_spec and err:
_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))
if format_spec and err:
_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))
if format_spec and err:
_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))
if format_spec and err:
_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))
if format_spec and err:
_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))
if format_spec and err:
_format_error(ret)
return ret