cosmopolitan/libc/fmt/unbing.c
Justine Tunney ea0b5d9d1c Get Cosmopolitan into releasable state
A new rollup tool now exists for flattening out the headers in a way
that works better for our purposes than cpp. A lot of the API clutter
has been removed. APIs that aren't a sure thing in terms of general
recommendation are now marked internal.

There's now a smoke test for the amalgamation archive and gigantic
header file. So we can now guarantee you can use this project on the
easiest difficulty setting without the gigantic repository.

A website is being created, which is currently a work in progress:
https://justine.storage.googleapis.com/cosmopolitan/index.html
2020-11-25 08:19:00 -08:00

82 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ This program is free software; you can redistribute it and/or modify │
│ it under the terms of the GNU General Public License as published by │
│ the Free Software Foundation; version 2 of the License. │
│ │
│ This program is distributed in the hope that it will be useful, but │
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
│ General Public License for more details. │
│ │
│ You should have received a copy of the GNU General Public License │
│ along with this program; if not, write to the Free Software │
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
│ 02110-1301 USA │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/alg/alg.h"
#include "libc/macros.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/str/str.h"
static const int kCp437iMultimappings[] = {
u'\n' << 8 | '\n', // NEWLINE
u'\r' << 8 | '\r', // CARRIAGE RETURN
u'?' << 8 | '?', // TRIGRAPH
u'\'' << 8 | '\'', // CHARACTER LITERAL
u'\"' << 8 | '\"', // STRING LITERAL
u'\\' << 8 | '\\', // ESCAPE LITERAL
u'' << 8 | '\0', // EMPTY SET
u'' << 8 | '\0', // SYMBOL FOR NULL [sic]
0x20A7 << 8 | 0x9E, // PESETA SIGN
u'Π' << 8 | 0xE3, // GREEK CAPITAL LETTER PI
u'' << 8 | 0xE3, // N-ARY PRODUCT
u'' << 8 | 0xE4, // N-ARY SUMMATION
u'µ' << 8 | 0xE6, // MICRO SIGN
u'' << 8 | 0xEA, // OHM SIGN
u'' << 8 | 0xEB, // PARTIAL DIFFERENTIAL
u'ϕ' << 8 | 0xED, // PHI SMALL (CLOSED FORM)
u'ε' << 8 | 0xEE, // LATIN SMALL LETTER EPSILON
u'' << 8 | 0xEE, // SMALL ELEMENT OF
u'' << 8 | 0xEE, // ELEMENT-OF SIGN
u'β' << 8 | 0xE1, // GREEK SMALL BETA
u'ſ' << 8 | 0xF4, // LATIN SMALL LETTER LONG S
};
static int g_cp437i[256 + ARRAYLEN(kCp437iMultimappings)];
/**
* Turns CP437 unicode glyph into its binary representation.
*
* @param c is a unicode character
* @return byte representation, or -1 if ch wasn't ibm cp437
* @see bing()
*/
int unbing(int c) {
int m, l, r;
l = 0;
r = ARRAYLEN(g_cp437i) - 1;
while (l <= r) {
m = (l + r) >> 1;
if ((g_cp437i[m] >> 8) < c) {
l = m + 1;
} else if ((g_cp437i[m] >> 8) > c) {
r = m - 1;
} else {
return g_cp437i[m] & 0xff;
}
}
return -1;
}
static textstartup void g_cp437i_init() {
unsigned i;
for (i = 0; i < 256; ++i) g_cp437i[i] = kCp437[i] << 8 | i;
memcpy(g_cp437i + 256, kCp437iMultimappings, sizeof(kCp437iMultimappings));
djbsort(g_cp437i, ARRAYLEN(g_cp437i));
}
const void *const g_cp437i_ctor[] initarray = {g_cp437i_init};