Class Utf8LdcChunker

java.lang.Object
org.ek9lang.compiler.backend.jvm.Utf8LdcChunker

final class Utf8LdcChunker extends Object
Splits an over-long String so it can be emitted as one-or-more JVM LDC constants without breaching the class-file CONSTANT_Utf8 ceiling.

A CONSTANT_Utf8 entry stores its length in a u2, so a single LDC string cannot exceed 65535 modified-UTF8 bytes (JVMS 4.4.7). The coverage/profiling per-module metadata JSON blows past this on large, cohesive modules (a real web service with inline dynamic response classes carries hundreds of probes) — see docs/ir-and-codegen/EK9_BUG_COVERAGE_METADATA_UTF8_OVERFLOW.md. The caller emits the string as chunks re-joined by a StringBuilder at runtime, so no single constant breaches the limit and the registration call still receives the whole string (no runtime-format change, and the self-contained per-class embedding is preserved).

Pure logic, unit-tested independently of the bytecode emission it feeds. "modified UTF-8" is the class-file encoding: each UTF-16 code unit encodes on its own (1 byte for U+0001..U+007F, 3 bytes for U+0800..U+FFFF, otherwise 2), so an unpaired surrogate at a chunk boundary round-trips verbatim and the re-joined string equals the original.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final int
    Hard JVM limit: a CONSTANT_Utf8 entry's length field is a u2.
    (package private) static final int
    Per-chunk target, with headroom below the hard limit so every emitted LDC stays safe.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static List<String>
    chunk(String s, int maxBytesPerChunk)
    Split into pieces whose modified-UTF8 length each stays <= maxBytesPerChunk, cutting only on char boundaries.
    (package private) static boolean
    True when the whole string fits in one LDC — the common fast path, no reassembly.
    (package private) static int
    Modified-UTF8 encoded length (the class-file CONSTANT_Utf8 encoding), per JVMS 4.4.7.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_UTF8_BYTES

      static final int MAX_UTF8_BYTES
      Hard JVM limit: a CONSTANT_Utf8 entry's length field is a u2.
      See Also:
    • SAFE_CHUNK_BYTES

      static final int SAFE_CHUNK_BYTES
      Per-chunk target, with headroom below the hard limit so every emitted LDC stays safe.
      See Also:
  • Method Details

    • modifiedUtf8Length

      static int modifiedUtf8Length(String s)
      Modified-UTF8 encoded length (the class-file CONSTANT_Utf8 encoding), per JVMS 4.4.7.
    • fitsInSingleLdc

      static boolean fitsInSingleLdc(String s)
      True when the whole string fits in one LDC — the common fast path, no reassembly.
    • chunk

      static List<String> chunk(String s, int maxBytesPerChunk)
      Split into pieces whose modified-UTF8 length each stays <= maxBytesPerChunk, cutting only on char boundaries. Concatenating the pieces reproduces the input exactly.
      Parameters:
      s - the string to split (may be any length)
      maxBytesPerChunk - per-chunk byte ceiling; must be >= 3 (a single char costs ≤ 3 bytes)
      Returns:
      the ordered chunks; joining them equals s