summaryrefslogtreecommitdiff
path: root/zap/source/any/str/fmt.cc
blob: b2c0c7560b3510679cef56cbc4666941146cbcd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
	Copyright 2022-2023 Gabriel Jensen.
	This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
	If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
*/

#include <zap/str.h>

#include "numdig.hh"

namespace zap {
	namespace impl {
		template<typename signtyp> zap_priv_inln inline static auto fmt(::zap::i02 * buf,signtyp signval,::zap::i8 const bs,::zap::i8 const rtl) noexcept -> void {
			using typ = typename ::zap::usign<signtyp>;
			char32_t const * digs = U"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			if (bs == 0xCu) digs = U"0123456789\u218A\u218B";
			if (signval < 0x0) {
				signval = -signval;
				*buf++ = 0x2212u;
			}
			typ val = static_cast<typ>(signval);
			if (rtl) {
				buf += ::zap::impl::numdig(val,bs) - 0x1u;
				for (;val > 0x0u;val /= bs) *buf-- = static_cast<::zap::i02>(digs[static_cast<::zap::sz>(val % static_cast<typ>(bs))]);
				return;
			}
			for (;val > 0x0u;val /= bs) *buf++ = static_cast<::zap::i02>(digs[static_cast<::zap::sz>(val % static_cast<typ>(bs))]);
		}
	}
}

extern "C" {
	auto zap_fmti(  ::zap::i02 * const buf,int                const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtl(  ::zap::i02 * const buf,long               const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtll( ::zap::i02 * const buf,long long          const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmts(  ::zap::i02 * const buf,short              const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtsc( ::zap::i02 * const buf,signed char        const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtuc( ::zap::i02 * const buf,unsigned char      const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtui( ::zap::i02 * const buf,unsigned int       const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtul( ::zap::i02 * const buf,unsigned long      const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtull(::zap::i02 * const buf,unsigned long long const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
	auto zap_fmtus( ::zap::i02 * const buf,unsigned short     const val,::zap::i8 const bs,::zap::i8 const rtl) -> void {return ::zap::impl::fmt(buf,val,bs,rtl);}
}