diff --git a/internal/cgen/base/fundamental-public.h b/internal/cgen/base/fundamental-public.h index d908b206..a8485009 100644 --- a/internal/cgen/base/fundamental-public.h +++ b/internal/cgen/base/fundamental-public.h @@ -1357,6 +1357,25 @@ wuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s, return wuffs_base__make_slice_u8(NULL, 0); } +// wuffs_base__table_u8__subtable_ij returns t[ix:jx, iy:jy]. +// +// It returns an empty table if i or j is out of bounds. +static inline wuffs_base__table_u8 // +wuffs_base__table_u8__subtable_ij(wuffs_base__table_u8 t, + uint64_t ix, + uint64_t iy, + uint64_t jx, + uint64_t jy) { + if ((ix <= jx) && (jx <= SIZE_MAX) && (jx <= t.width) && // + (iy <= jy) && (jy <= SIZE_MAX) && (jy <= t.height)) { + return wuffs_base__make_table_u8(t.ptr + ix + (iy * t.stride), // + ((size_t)(jx - ix)), // + ((size_t)(jy - iy)), // + t.stride); // + } + return wuffs_base__make_table_u8(NULL, 0, 0, 0); +} + // wuffs_base__table__flattened_length returns the number of elements covered // by the 1-dimensional span that backs a 2-dimensional table. This counts the // elements inside the table and, when width != stride, the elements outside diff --git a/internal/cgen/builtin.go b/internal/cgen/builtin.go index 8b032d15..fa6638ca 100644 --- a/internal/cgen/builtin.go +++ b/internal/cgen/builtin.go @@ -1034,6 +1034,15 @@ func (g *gen) writeBuiltinTable(b *buffer, recv *a.Expr, method t.ID, args []*a. } b.writes(", ") return g.writeArgs(b, args, depth) + + case t.IDSubtable: + // TODO: don't assume that the table is a table of base.u8. + b.writes("wuffs_base__table_u8__subtable_ij(") + if err := g.writeExpr(b, recv, false, depth); err != nil { + return err + } + b.writes(", ") + return g.writeArgs(b, args, depth) } if field != "" { diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 99c62574..91d0c9c8 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -964,6 +964,8 @@ var TableFuncs = []string{ "GENERIC T2.width() u64", "GENERIC T2.row(y: u32) T1", + "GENERIC T2.subtable(" + + "min_incl_x: u64, min_incl_y: u64, max_incl_x: u64, max_incl_y: u64) T2", } func ParseFuncs(tm *t.Map, ss []string, callback func(*a.Func) error) error { diff --git a/lang/token/list.go b/lang/token/list.go index aab8656b..313446e4 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -670,10 +670,12 @@ const ( IDPrefix = ID(0x244) IDRow = ID(0x245) IDStride = ID(0x246) - IDSuffix = ID(0x247) - IDUintptrLow12Bits = ID(0x248) - IDValidUTF8Length = ID(0x249) - IDWidth = ID(0x24A) + IDSubslice = ID(0x247) + IDSubtable = ID(0x248) + IDSuffix = ID(0x249) + IDUintptrLow12Bits = ID(0x24A) + IDValidUTF8Length = ID(0x24B) + IDWidth = ID(0x24C) IDLimitedSwizzleU32InterleavedFromReader = ID(0x280) IDSwizzleInterleavedFromReader = ID(0x281) @@ -1094,6 +1096,8 @@ var builtInsByID = [nBuiltInIDs]string{ IDPrefix: "prefix", IDRow: "row", IDStride: "stride", + IDSubslice: "subslice", + IDSubtable: "subtable", IDSuffix: "suffix", IDUintptrLow12Bits: "uintptr_low_12_bits", IDValidUTF8Length: "valid_utf_8_length", diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 62a1c80b..8d5773fb 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -1612,6 +1612,25 @@ wuffs_base__slice_u8__subslice_ij(wuffs_base__slice_u8 s, return wuffs_base__make_slice_u8(NULL, 0); } +// wuffs_base__table_u8__subtable_ij returns t[ix:jx, iy:jy]. +// +// It returns an empty table if i or j is out of bounds. +static inline wuffs_base__table_u8 // +wuffs_base__table_u8__subtable_ij(wuffs_base__table_u8 t, + uint64_t ix, + uint64_t iy, + uint64_t jx, + uint64_t jy) { + if ((ix <= jx) && (jx <= SIZE_MAX) && (jx <= t.width) && // + (iy <= jy) && (jy <= SIZE_MAX) && (jy <= t.height)) { + return wuffs_base__make_table_u8(t.ptr + ix + (iy * t.stride), // + ((size_t)(jx - ix)), // + ((size_t)(jy - iy)), // + t.stride); // + } + return wuffs_base__make_table_u8(NULL, 0, 0, 0); +} + // wuffs_base__table__flattened_length returns the number of elements covered // by the 1-dimensional span that backs a 2-dimensional table. This counts the // elements inside the table and, when width != stride, the elements outside