interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_gpu_fmt.c

81 lines · 3003 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* omega_gpu_fmt.c -- see omega_gpu_fmt.h. Freestanding: stdarg only. */
18#include "omega_gpu_fmt.h"
19#include <stdarg.h>
20#include <stdint.h>
21
22typedef struct { char *out; size_t cap, n; } Sink;
23
24static void put(Sink *s, char c) {
25 if (s->n + 1 < s->cap) s->out[s->n++] = c;
26}
27static void put_str(Sink *s, const char *p) {
28 if (!p) p = "(null)";
29 while (*p) put(s, *p++);
30}
31static void put_u(Sink *s, unsigned long long v, unsigned base) {
32 char tmp[24];
33 int i = 0;
34 do { unsigned d = (unsigned)(v % base); tmp[i++] = (char)(d < 10 ? '0' + d : 'a' + d - 10); v /= base; } while (v);
35 while (i) put(s, tmp[--i]);
36}
37static void put_d(Sink *s, long long v) {
38 if (v < 0) { put(s, '-'); put_u(s, (unsigned long long)(-(v + 1)) + 1ull, 10); }
39 else put_u(s, (unsigned long long)v, 10);
40}
41
42int omega_gpu_fmt(char *out, size_t cap, const char *fmt, ...) {
43 Sink s = { out, cap, 0 };
44 va_list ap;
45 if (!out || cap == 0) return 0;
46 if (!fmt) fmt = "";
47 va_start(ap, fmt);
48 for (const char *p = fmt; *p; p++) {
49 if (*p != '%') { put(&s, *p); continue; }
50 const char *start = p++;
51 int l = 0, z = 0;
52 if (*p == 'z') { z = 1; p++; }
53 else while (*p == 'l' && l < 2) { l++; p++; }
54 switch (*p) {
55 case 's': put_str(&s, va_arg(ap, const char *)); break;
56 case 'c': put(&s, (char)va_arg(ap, int)); break;
57 case 'd':
58 if (z) put_d(&s, (long long)va_arg(ap, ptrdiff_t));
59 else if (l == 2) put_d(&s, va_arg(ap, long long));
60 else if (l == 1) put_d(&s, va_arg(ap, long));
61 else put_d(&s, va_arg(ap, int));
62 break;
63 case 'u': case 'x': {
64 unsigned base = *p == 'x' ? 16u : 10u;
65 if (z) put_u(&s, va_arg(ap, size_t), base);
66 else if (l == 2) put_u(&s, va_arg(ap, unsigned long long), base);
67 else if (l == 1) put_u(&s, va_arg(ap, unsigned long), base);
68 else put_u(&s, va_arg(ap, unsigned int), base);
69 break;
70 }
71 case '%': put(&s, '%'); break;
72 default: /* unsupported conversion: copy it literally, consume nothing */
73 for (const char *q = start; q <= p && *q; q++) put(&s, *q);
74 if (!*p) p--;
75 break;
76 }
77 }
78 va_end(ap);
79 s.out[s.n] = '\0';
80 return (int)s.n;
81}