mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-01 16:58:30 +00:00
Add scouts honor escape hatch for source embedding
This commit is contained in:
parent
c91b3c5006
commit
b4269930f7
547 changed files with 1516 additions and 944 deletions
|
@ -1,10 +1,17 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_RUNTIME_APPENDCHAR_H_
|
||||
#define COSMOPOLITAN_LIBC_RUNTIME_APPENDCHAR_H_
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpenc.h"
|
||||
#include "libc/str/tpencode.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
forceinline void AppendChar(char **p, char *pe, wint_t c) {
|
||||
if (*p < pe) *p += tpencode(*p, pe - *p, c, false);
|
||||
static inline void AppendChar(char **p, char *pe, wint_t wc) {
|
||||
uint64_t w;
|
||||
w = tpenc(wc);
|
||||
do {
|
||||
if (*p >= pe) break;
|
||||
*(*p)++ = w & 0xff;
|
||||
} while (w >>= 8);
|
||||
}
|
||||
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/macros.h"
|
||||
.yoink __FILE__
|
||||
.source __FILE__
|
||||
|
||||
/ Thompson-Pike Decoder Ring.
|
||||
/
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/limits.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpdecode.h"
|
||||
|
||||
compatfn int mbtowc(wchar_t *wc, const char *s, size_t n) {
|
||||
if (!s) return 0;
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/macros.h"
|
||||
.yoink __FILE__
|
||||
.source __FILE__
|
||||
|
||||
/ Returns pointer to last instance of character the BSD way.
|
||||
/
|
||||
|
|
|
@ -68,9 +68,6 @@ void *isnotplaintext(const void *, size_t) nothrow nocallback nosideeffect;
|
|||
#define UTF16_MOAR 0b1101100000000000 /* 0xD800..0xDBFF */
|
||||
#define UTF16_CONT 0b1101110000000000 /* 0xDC00..0xDBFF */
|
||||
|
||||
unsigned tpencode(char *buf, size_t size, wint_t c, bool32 awesome)
|
||||
paramsnonnull() libcesque;
|
||||
int tpdecode(const char *s, wint_t *out) paramsnonnull((1)) libcesque;
|
||||
unsigned getutf16(const char16_t *p, wint_t *wc);
|
||||
int pututf16(char16_t *s, size_t size, wint_t wc, bool awesome);
|
||||
int iswalnum(wint_t);
|
||||
|
@ -532,45 +529,11 @@ extern int (*const hook$wcsncmp)(const wchar_t *, const wchar_t *, size_t);
|
|||
__builtin_strlen(s), _strlen(s))
|
||||
#endif /* C11+ */
|
||||
|
||||
#define tpencode(BUF, SIZE, CH, AWESOME) __tpencode(BUF, SIZE, CH, AWESOME)
|
||||
#define pututf16(BUF, SIZE, CH, AWESOME) __pututf16(BUF, SIZE, CH, AWESOME)
|
||||
#define getutf16(BUF, CHPTR) __getutf16(BUF, CHPTR)
|
||||
#define tpdecode(S, OUT) __tpdecode(S, OUT)
|
||||
size_t _strlen(const char *s) asm("strlen") strlenesque;
|
||||
char *_strchr(const char *, int) asm("strchr") strlenesque;
|
||||
void *_memchr(const void *, int, size_t) asm("memchr") strlenesque;
|
||||
forceinline unsigned __tpencode(char *s, size_t size, wint_t wc,
|
||||
bool32 awesome) {
|
||||
unsigned char *p = (unsigned char *)s;
|
||||
if (size >= 1 && (0x00 <= wc && wc <= 0x7f)) {
|
||||
if (wc >= 32 || !awesome) {
|
||||
p[0] = (unsigned char)wc;
|
||||
return 1;
|
||||
} else if (size >= 2) {
|
||||
p[0] = 0xc0;
|
||||
p[1] = 0x80 | (unsigned char)wc;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
unsigned ax;
|
||||
asm("call\ttpencode"
|
||||
: "=a"(ax), "=m"(*(char(*)[size])s)
|
||||
: "D"(s), "S"(size), "d"(wc)
|
||||
: "cc");
|
||||
return ax;
|
||||
}
|
||||
forceinline int __tpdecode(const char *s, wint_t *out) {
|
||||
if (0 <= *s && *s <= 0x7f) {
|
||||
*out = *s;
|
||||
return 1;
|
||||
}
|
||||
int ax;
|
||||
asm("call\ttpdecode"
|
||||
: "=a"(ax), "=m"(*(char(*)[6])s)
|
||||
: "D"(s), "S"(out)
|
||||
: "cc");
|
||||
return ax;
|
||||
}
|
||||
forceinline int __pututf16(char16_t *s, size_t size, wint_t wc,
|
||||
bool32 awesome) {
|
||||
if (size >= 1 && (0x00 <= wc && wc <= 0xD7FF)) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define COSMOPOLITAN_LIBC_STR_STRCMP8TO16I_H_
|
||||
#include "libc/conv/conv.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpdecode.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
|
|
26
libc/str/tpdecode.h
Normal file
26
libc/str/tpdecode.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_STR_TPDECODE_H_
|
||||
#define COSMOPOLITAN_LIBC_STR_TPDECODE_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
int tpdecode(const char *, wint_t *) paramsnonnull((1)) libcesque;
|
||||
|
||||
#ifndef __STRICT_ANSI__
|
||||
#define tpdecode(S, OUT) __tpdecode(S, OUT)
|
||||
forceinline int __tpdecode(const char *s, wint_t *out) {
|
||||
if (0 <= *s && *s <= 0x7f) {
|
||||
*out = *s;
|
||||
return 1;
|
||||
}
|
||||
int ax;
|
||||
asm("call\ttpdecode"
|
||||
: "=a"(ax), "=m"(*(char(*)[6])s)
|
||||
: "D"(s), "S"(out)
|
||||
: "cc");
|
||||
return ax;
|
||||
}
|
||||
#endif
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_STR_TPDECODE_H_ */
|
|
@ -19,6 +19,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/errno.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpdecode.h"
|
||||
#include "libc/str/tpdecodecb.h"
|
||||
|
||||
forceinline int getbyte(void *arg, uint32_t i) {
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/macros.h"
|
||||
.yoink __FILE__
|
||||
.source __FILE__
|
||||
|
||||
/ Encodes Thompson-Pike varint.
|
||||
/
|
||||
|
|
|
@ -4,19 +4,20 @@
|
|||
COSMOPOLITAN_C_START_
|
||||
|
||||
uint64_t tpenc(int32_t) pureconst;
|
||||
uint64_t tpenc2(int32_t) pureconst;
|
||||
|
||||
#define tpenc(CODE) \
|
||||
({ \
|
||||
unsigned long Buf; \
|
||||
int Di, Code = (CODE); \
|
||||
if (0 <= Code && Code <= 127) { \
|
||||
Buf = Code; \
|
||||
} else { \
|
||||
asm("call\ttpenc" : "=a"(Buf), "=D"(Di) : "1"(CODE) : "cc"); \
|
||||
} \
|
||||
Buf; \
|
||||
#ifndef __STRICT_ANSI__
|
||||
#define tpenc(CODE) \
|
||||
({ \
|
||||
long Buf; \
|
||||
int32_t Code = (CODE); \
|
||||
if (0 <= Code && Code <= 127) { \
|
||||
Buf = Code; \
|
||||
} else { \
|
||||
asm("call\ttpenc" : "=a"(Buf), "+D"(Code) : /* inputs */ : "cc"); \
|
||||
} \
|
||||
Buf; \
|
||||
})
|
||||
#endif
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
29
libc/str/tpencode.h
Normal file
29
libc/str/tpencode.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_STR_TPENCODE_H_
|
||||
#define COSMOPOLITAN_LIBC_STR_TPENCODE_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
unsigned tpencode(char *, size_t, wint_t, bool32) paramsnonnull() libcesque;
|
||||
|
||||
#ifndef __STRICT_ANSI__
|
||||
#define tpencode(...) __tpencode(__VA_ARGS__)
|
||||
forceinline unsigned __tpencode(char *p, size_t size, wint_t wc,
|
||||
bool32 awesome) {
|
||||
if (size >= 1 && (0x00 <= wc && wc <= 0x7f)) {
|
||||
if (wc >= 32 || !awesome) {
|
||||
p[0] = wc;
|
||||
return 1;
|
||||
} else if (size >= 2) {
|
||||
p[0] = 0xc0;
|
||||
p[1] = 0x80;
|
||||
p[1] |= wc;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
return (tpencode)(p, size, wc, awesome);
|
||||
}
|
||||
#endif
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_STR_TPENCODE_H_ */
|
|
@ -18,7 +18,8 @@
|
|||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpenc.h"
|
||||
#include "libc/str/tpencode.h"
|
||||
|
||||
/**
|
||||
* Thompson-Pike Varint Encoder.
|
||||
|
@ -27,29 +28,27 @@
|
|||
* is only called for non-ASCII, or DCE'd entirely. In addition to that
|
||||
* this function makes a promise to not clobber any registers but %rax.
|
||||
*
|
||||
* @param buf is what ch gets encoded to
|
||||
* @param p is what ch gets encoded to
|
||||
* @param size is the number of bytes available in buf
|
||||
* @param ch is a 32-bit integer
|
||||
* @param awesome mode enables numbers the IETF unilaterally banned
|
||||
* @return number of bytes written
|
||||
* @note this encoding was designed on a napkin in a new jersey diner
|
||||
*/
|
||||
unsigned(tpencode)(char *buf, size_t size, wint_t ch, bool32 awesome) {
|
||||
unsigned char *p = (unsigned char *)buf;
|
||||
if ((0 <= ch && ch < 32) && awesome && size >= 2) {
|
||||
unsigned(tpencode)(char *p, size_t size, wint_t wc, bool32 awesome) {
|
||||
int i, j;
|
||||
unsigned long w;
|
||||
if ((0 <= wc && wc < 32) && awesome && size >= 2) {
|
||||
p[0] = 0xc0;
|
||||
p[1] = 0x80 | (unsigned char)ch;
|
||||
p[1] = 0x80;
|
||||
p[1] |= wc;
|
||||
return 2;
|
||||
}
|
||||
struct TpEncode op = UseTpDecoderRing(ch);
|
||||
size_t i = op.len;
|
||||
if (op.len <= size) {
|
||||
for (;;) {
|
||||
p[--i] = (unsigned char)(0b10000000 | (ch & 0b00111111));
|
||||
if (!i) break;
|
||||
ch >>= 6;
|
||||
}
|
||||
p[0] = op.mark | (unsigned char)ch;
|
||||
}
|
||||
return op.len;
|
||||
i = 0;
|
||||
w = tpenc(wc);
|
||||
do {
|
||||
if (!size--) break;
|
||||
p[i++] = w & 0xff;
|
||||
} while (w >>= 8);
|
||||
return i;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/conv/conv.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpencode.h"
|
||||
|
||||
/**
|
||||
* Transcodes UTF-16 to UTF-8.
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/conv/conv.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpdecode.h"
|
||||
|
||||
/**
|
||||
* Transcodes UTF-8 to UTF-16.
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/limits.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpencode.h"
|
||||
|
||||
size_t wcrtomb(char *s, wchar_t wc, mbstate_t *st) {
|
||||
if (!s) return 1;
|
||||
|
|
|
@ -19,10 +19,13 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/conv/conv.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpencode.h"
|
||||
|
||||
size_t wcsrtombs(char *dest, const wchar_t **src, size_t len, mbstate_t *ps) {
|
||||
/* TODO(jart): broken broken broken insane api */
|
||||
size_t i = 0;
|
||||
/* TODO(jart): broken */
|
||||
int64_t word;
|
||||
size_t i, got;
|
||||
i = 0;
|
||||
if (len) {
|
||||
for (;;) {
|
||||
if (!**src || len == 1) {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/limits.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/tpencode.h"
|
||||
|
||||
int wctomb(char *s, wchar_t wc) {
|
||||
if (!s) return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue