private void writeSubstring(Kim kim) throws JSONException {
        this.substringkeep.reserve();
        zero();
        int from = 0;
        int thru = kim.length;
        int until = thru - JSONzip.minSubstringLength;
        int previousFrom = none;
        int previousThru = 0;

// Find a substring from the substring keep.

        while (true) {
            int at;
            int integer = none;
            for (at = from; at <= until; at += 1) {
                integer = this.substringkeep.match(kim, at, thru);
                if (integer != none) {
                    break;
                }
            }
            if (integer == none) {
                break;
            }

// If a substring is found, emit any characters that were before the matched
// substring. Then emit the substring's integer and loop back to match the
// remainder with another substring.

            if (from != at) {
                zero();
                write(kim, from, at, this.substringhuff);
                write(end, this.substringhuff);
                if (previousFrom != none) {
                    this.substringkeep.registerOne(kim, previousFrom,
                            previousThru);
                    previousFrom = none;
                }
            }
            one();
            writeAndTick(integer, this.substringkeep);
            from = at + this.substringkeep.length(integer);
            if (previousFrom != none) {
                this.substringkeep.registerOne(kim, previousFrom,
                        previousThru);
                previousFrom = none;
            }
            previousFrom = at;
            previousThru = from + 1;
        }

// If a substring is not found, then emit the remaining characters.

        zero();
        if (from < thru) {
            write(kim, from, thru, this.substringhuff);
            if (previousFrom != none) {
                this.substringkeep.registerOne(kim, previousFrom, previousThru);
            }
        }
        write(end, this.substringhuff);
        zero();

// Register the string's substrings in the trie in hopes of future substring
// matching.

        substringkeep.registerMany(kim);
    }