private static void writeSubstringFilter(
            BerBuffer buffer,
            byte[] filter,
            int typeStart,
            int typeEnd,
            int valueStart,
            int valueEnd) {
        buffer.beginSequence(TYPE_SUBSTRING);

        buffer.writeOctetString(filter, typeStart, typeEnd - typeStart);
        buffer.beginSequence();

        int index;
        int previousIndex = valueStart;
        while ((index = findUnescaped(filter, previousIndex, valueEnd)) != -1) {
            if (previousIndex == valueStart) {
                if (previousIndex < index) {
                    byte[] filterValue = unescapeFilterValue(filter, previousIndex, index);
                    if (filterValue == null) {
                        buffer.writeOctetString(SUBSTRING_INITIAL,
                                filter,
                                previousIndex,
                                index - previousIndex);
                    } else {
                        buffer.writeOctetString(SUBSTRING_INITIAL, filterValue);
                    }
                }
            } else {
                if (previousIndex < index) {
                    byte[] filterValue = unescapeFilterValue(filter, previousIndex, index);
                    if (filterValue == null) {
                        buffer.writeOctetString(SUBSTRING_ANY,
                                filter,
                                previousIndex,
                                index - previousIndex);
                    } else {
                        buffer.writeOctetString(SUBSTRING_ANY, filterValue);
                    }
                }
            }
            previousIndex = index + 1;
        }
        if (previousIndex < valueEnd) {
            byte[] filterValue = unescapeFilterValue(filter, previousIndex, valueEnd);
            if (filterValue == null) {
                buffer.writeOctetString(SUBSTRING_FINAL,
                        filter,
                        previousIndex,
                        valueEnd - previousIndex);
            } else {
                buffer.writeOctetString(SUBSTRING_FINAL, filterValue);
            }
        }
        buffer.endSequence()
                .endSequence();
    }
@author James Chen
@implSpec <a href="https://tools.ietf.org/html/rfc4515"> RFC 4515: String Representation of
          Search Filters</a>
turms-gateway/src/main/java/im/turms/gateway/infra/ldap/element/operation/search/Filter.java