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 f4323a80a2
Fix miscellaneous issues (#85)
* Fix #75

* Build on macOS 11 for #72

* Fix #76

* Fix test

* Fix generator example in docs

* Fix Python/Cython packaging; Fix #80

* Fix tests

* Fix tests

* Fix tests

* Fix tests

* Fix syntax

* Fix CI

* Fix CI

* Fix CI; Dominate imported bindings

* Fix CI; Dominate imported bindings

* Fix .gitignore

* Fix tests

* Fix rm command

* Format

* clang-format

* Organize and optimize string formatting

* Fix format error message

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
2022-12-12 20:54:01 -05:00

59 lines
1.5 KiB
Python

# Copyright (C) 2022 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 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 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 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 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 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 err:
_format_error(ret)
return ret