mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Add a slice.suffix method.
This commit is contained in:
1 parent
51f2b183a2
commit
2fb9972bb9
10 files changed
+176
-2
No files matched your search
@@ -40,4 +40,43 @@ extern int printf(const char* __restrict __format, ...);
|
||||
#define PUFFS_UNLIKELY(expr) (expr)
|
||||
#endif
|
||||
|
||||
// ---------------- Static Inline Functions
|
||||
//
|
||||
// The helpers below are functions, instead of macros, because their arguments
|
||||
// can be an expression that we shouldn't evaluate more than once.
|
||||
//
|
||||
// They are in base-impl.h and hence copy/pasted into every generated C file,
|
||||
// instead of being in some "base.c" file, since a design goal is that users of
|
||||
// the generated C code can often just #include a single .c file, such as
|
||||
// "gif.c", without having to additionally include or otherwise build and link
|
||||
// a "base.c" file.
|
||||
//
|
||||
// They are static, so that linking multiple puffs .o files won't complain about
|
||||
// duplicate function definitions.
|
||||
//
|
||||
// They are explicitly marked inline, even if modern compilers don't use the
|
||||
// inline attribute to guide optimizations such as inlining, to avoid the
|
||||
// -Wunused-function warning, and we like to compile with -Wall -Werror.
|
||||
|
||||
// puffs_base_slice_u8_prefix returns up to the first n bytes of s.
|
||||
static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(
|
||||
puffs_base_slice_u8 s,
|
||||
uint64_t n) {
|
||||
if ((uint64_t)(s.len) > n) {
|
||||
s.len = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// puffs_base_slice_u8_suffix returns up to the last n bytes of s.
|
||||
static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
||||
puffs_base_slice_u8 s,
|
||||
uint64_t n) {
|
||||
if ((uint64_t)(s.len) > n) {
|
||||
s.ptr += (uint64_t)(s.len) - n;
|
||||
s.len = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
@@ -13,7 +13,9 @@ const baseHeader = "" +
|
||||
|
||||
const baseImpl = "" +
|
||||
"#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro in" +
|
||||
"stead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
"stead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n// ---------------- Static Inline Functions\n//\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n//" +
|
||||
" the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple puffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n retu" +
|
||||
"rn s;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
|
||||
type template_args_short_read struct {
|
||||
|
||||
@@ -134,6 +134,20 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if isIsPrefixSuffix(g.tm, n, t.KeySuffix) {
|
||||
// TODO: don't assume that the slice is a slice of u8.
|
||||
b.writes("puffs_base_slice_u8_suffix(")
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(", ")
|
||||
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")")
|
||||
return nil
|
||||
}
|
||||
if isInDst(g.tm, n, t.KeySlice, 0) {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
|
||||
@@ -760,6 +760,14 @@ func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(lw:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
|
||||
@@ -324,6 +324,45 @@ extern int printf(const char* __restrict __format, ...);
|
||||
#define PUFFS_UNLIKELY(expr) (expr)
|
||||
#endif
|
||||
|
||||
// ---------------- Static Inline Functions
|
||||
//
|
||||
// The helpers below are functions, instead of macros, because their arguments
|
||||
// can be an expression that we shouldn't evaluate more than once.
|
||||
//
|
||||
// They are in base-impl.h and hence copy/pasted into every generated C file,
|
||||
// instead of being in some "base.c" file, since a design goal is that users of
|
||||
// the generated C code can often just #include a single .c file, such as
|
||||
// "gif.c", without having to additionally include or otherwise build and link
|
||||
// a "base.c" file.
|
||||
//
|
||||
// They are static, so that linking multiple puffs .o files won't complain about
|
||||
// duplicate function definitions.
|
||||
//
|
||||
// They are explicitly marked inline, even if modern compilers don't use the
|
||||
// inline attribute to guide optimizations such as inlining, to avoid the
|
||||
// -Wunused-function warning, and we like to compile with -Wall -Werror.
|
||||
|
||||
// puffs_base_slice_u8_prefix returns up to the first n bytes of s.
|
||||
static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(
|
||||
puffs_base_slice_u8 s,
|
||||
uint64_t n) {
|
||||
if ((uint64_t)(s.len) > n) {
|
||||
s.len = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// puffs_base_slice_u8_suffix returns up to the last n bytes of s.
|
||||
static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
||||
puffs_base_slice_u8 s,
|
||||
uint64_t n) {
|
||||
if ((uint64_t)(s.len) > n) {
|
||||
s.ptr += (uint64_t)(s.len) - n;
|
||||
s.len = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
|
||||
// ---------------- Status Codes Implementations
|
||||
@@ -589,6 +628,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
v_written = ((puffs_base_slice_u8){.ptr = a_dst.buf->ptr,
|
||||
.len = b_wptr_dst - a_dst.buf->ptr});
|
||||
if (((uint64_t)(v_written.len)) >= 32768) {
|
||||
v_written = puffs_base_slice_u8_suffix(v_written, 32768);
|
||||
}
|
||||
}
|
||||
status = v_z;
|
||||
|
||||
@@ -297,6 +297,45 @@ extern int printf(const char* __restrict __format, ...);
|
||||
#define PUFFS_UNLIKELY(expr) (expr)
|
||||
#endif
|
||||
|
||||
// ---------------- Static Inline Functions
|
||||
//
|
||||
// The helpers below are functions, instead of macros, because their arguments
|
||||
// can be an expression that we shouldn't evaluate more than once.
|
||||
//
|
||||
// They are in base-impl.h and hence copy/pasted into every generated C file,
|
||||
// instead of being in some "base.c" file, since a design goal is that users of
|
||||
// the generated C code can often just #include a single .c file, such as
|
||||
// "gif.c", without having to additionally include or otherwise build and link
|
||||
// a "base.c" file.
|
||||
//
|
||||
// They are static, so that linking multiple puffs .o files won't complain about
|
||||
// duplicate function definitions.
|
||||
//
|
||||
// They are explicitly marked inline, even if modern compilers don't use the
|
||||
// inline attribute to guide optimizations such as inlining, to avoid the
|
||||
// -Wunused-function warning, and we like to compile with -Wall -Werror.
|
||||
|
||||
// puffs_base_slice_u8_prefix returns up to the first n bytes of s.
|
||||
static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(
|
||||
puffs_base_slice_u8 s,
|
||||
uint64_t n) {
|
||||
if ((uint64_t)(s.len) > n) {
|
||||
s.len = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// puffs_base_slice_u8_suffix returns up to the last n bytes of s.
|
||||
static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(
|
||||
puffs_base_slice_u8 s,
|
||||
uint64_t n) {
|
||||
if ((uint64_t)(s.len) > n) {
|
||||
s.ptr += (uint64_t)(s.len) - n;
|
||||
s.len = n;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // PUFFS_BASE_IMPL_H
|
||||
|
||||
// ---------------- Status Codes Implementations
|
||||
|
||||
@@ -689,7 +689,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
return zero, bitMask(int(aMax.Int64())), nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.is_suspension(etc)".
|
||||
if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) {
|
||||
if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) || isIsPrefixSuffix(q.tm, n, t.KeySuffix) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
|
||||
@@ -410,6 +410,23 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(typeExprBool)
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.suffix(etc)".
|
||||
if isIsPrefixSuffix(q.tm, n, t.KeySuffix) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
n.LHS().SetTypeChecked()
|
||||
n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK.
|
||||
for _, o := range n.Args() {
|
||||
if err := q.tcheckArg(o.Arg(), depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// TODO: don't assume that the slice is a slice of u8.
|
||||
n.SetMType(typeExprSliceU8)
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
if isSetLiteralWidth(q.tm, n) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
@@ -597,6 +614,14 @@ func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(lw:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
|
||||
@@ -271,6 +271,8 @@ const (
|
||||
KeyIsSuspension = Key(IDIsSuspension >> KeyShift)
|
||||
KeySlice = Key(IDSlice >> KeyShift)
|
||||
KeyLength = Key(IDLength >> KeyShift)
|
||||
KeyPrefix = Key(IDPrefix >> KeyShift)
|
||||
KeySuffix = Key(IDSuffix >> KeyShift)
|
||||
|
||||
KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift)
|
||||
KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift)
|
||||
@@ -446,6 +448,8 @@ const (
|
||||
IDIsSuspension = ID(0xC7<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSlice = ID(0xC8<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDLength = ID(0xC9<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDPrefix = ID(0xCA<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
IDSuffix = ID(0xCB<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
|
||||
)
|
||||
|
||||
// The IDXFoo IDs are not returned by the tokenizer. They are used by the
|
||||
@@ -643,6 +647,8 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyIsSuspension: {"is_suspension", IDIsSuspension},
|
||||
KeySlice: {"slice", IDSlice},
|
||||
KeyLength: {"length", IDLength},
|
||||
KeyPrefix: {"prefix", IDPrefix},
|
||||
KeySuffix: {"suffix", IDSuffix},
|
||||
}
|
||||
|
||||
var builtInsByName = map[string]ID{}
|
||||
|
||||
@@ -100,6 +100,7 @@ pub func decoder.decode?(dst writer1, src reader1)() {
|
||||
// change the veracity of any in.dst assertions?
|
||||
var written[] u8 = in.dst.slice()
|
||||
if written.length() >= (32 * 1024) {
|
||||
written = written.suffix(up_to:32 * 1024)
|
||||
}
|
||||
}
|
||||
return z
|
||||
|
||||
Reference in new issue
Block a user