private static void writeFilter(
            BerBuffer buffer,
            byte[] filter,
            int filterStartIndex,
            int filterEndIndex) {
        int equalIndex = ByteUtil.indexOf(filter, '=', filterStartIndex, filterEndIndex);
        if (equalIndex == -1) {
            throw new LdapException(ResultCode.FILTER_ERROR, "Missing \"=\"");
        }
        int filterTypeEndIndex;
        int valueStartIndex = equalIndex + 1;
        int filterType;
        switch (filter[equalIndex - 1]) {
            case '<' -> {
                filterType = TYPE_LESS;
                filterTypeEndIndex = equalIndex - 1;
            }
            case '>' -> {
                filterType = TYPE_GREATER;
                filterTypeEndIndex = equalIndex - 1;
            }
            case '~' -> {
                filterType = TYPE_APPROXIMATE;
                filterTypeEndIndex = equalIndex - 1;
            }
            case ':' -> {
                filterType = TYPE_EXTENSIBLE_MATCH;
                filterTypeEndIndex = equalIndex - 1;
            }
            default -> {
                filterType = -1;
                filterTypeEndIndex = equalIndex;
            }
        }
        byte lastChar = filter[filterTypeEndIndex - 1];
        if (lastChar == '.' || lastChar == ';' || lastChar == ':') {
            throw new LdapException(
                    ResultCode.FILTER_ERROR,
                    "Invalid attribute description: The last character must not be a \".\", \";\", or \":\"");
        }
        int attributeOptionsStartIndex = -1;
        // ":"
        int extensibleMatchStartIndex = -1;
        char firstFilterChar = (char) filter[filterStartIndex];
        if (isAlphabeticOrDigit(firstFilterChar)) {
            boolean isNumericOid = firstFilterChar <= '9';
            for (int i = filterStartIndex + 1; i < filterTypeEndIndex; i++) {
                char currentChar = (char) filter[i];
                if (currentChar == ';') {
                    if (isNumericOid && filter[i - 1] == '.') {
                        throw new LdapException(
                                ResultCode.FILTER_ERROR,
                                "Invalid attribute description");
                    }
                    attributeOptionsStartIndex = i;
                    break;
                }
                if (currentChar == ':' && filterType == TYPE_EXTENSIBLE_MATCH) {
                    if (isNumericOid && filter[i - 1] == '.') {
                        throw new LdapException(
                                ResultCode.FILTER_ERROR,
                                "Invalid attribute description");
                    }
                    extensibleMatchStartIndex = i;
                    break;
                }
                validate(filter, isNumericOid, i);
            }
        } else if (filterType == TYPE_EXTENSIBLE_MATCH && firstFilterChar == ':') {
            extensibleMatchStartIndex = filterStartIndex;
        } else {
            throw new LdapException(ResultCode.FILTER_ERROR, "Invalid attribute description");
        }
        if (attributeOptionsStartIndex > 0) {
            for (int i = attributeOptionsStartIndex + 1; i < filterTypeEndIndex; i++) {
                byte previousFilterChar = filter[i - 1];
                byte filterChar = filter[i];
                if (filterChar == ';') {
                    if (previousFilterChar == ';') {
                        throw new LdapException(
                                ResultCode.FILTER_ERROR,
                                "Invalid attribute description: Consecutive \";\" are not allowed");
                    }
                    continue;
                }
                if (filterChar == ':' && filterType == TYPE_EXTENSIBLE_MATCH) {
                    if (previousFilterChar == ';') {
                        throw new LdapException(
                                ResultCode.FILTER_ERROR,
                                "Invalid attribute description: \";\" should not be followed by \":\"");
                    }
                    extensibleMatchStartIndex = i;
                    break;
                }
                validateAttributeDescription((char) filter[i]);
            }
        }

        if (extensibleMatchStartIndex > 0) {
            boolean hasMatchingRule = false;
            for (int i = extensibleMatchStartIndex + 1; i < filterTypeEndIndex; i++) {
                char c = (char) filter[i];
                if (c == ':') {
                    throw new LdapException(
                            ResultCode.FILTER_ERROR,
                            "Invalid attribute description: multiple matching rules are not allowed");
                }
                if (!isAlphabeticOrDigit(c)) {
                    throw new LdapException(
                            ResultCode.FILTER_ERROR,
                            "Invalid attribute description");
                }
                boolean isNumericOid = c <= '9';
                i++;
                for (int j = i; j < filterTypeEndIndex; j++, i++) {
                    if (filter[j] == ':') {
                        if (hasMatchingRule) {
                            throw new LdapException(
                                    ResultCode.FILTER_ERROR,
                                    "Invalid attribute description: multiple matching rules are not allowed");
                        }
                        if (isNumericOid && filter[j - 1] == '.') {
                            throw new LdapException(
                                    ResultCode.FILTER_ERROR,
                                    "Invalid attribute description");
                        }
                        hasMatchingRule = true;
                        break;
                    }
                    validate(filter, isNumericOid, j);
                }
            }
        }
        if (filterTypeEndIndex == equalIndex) {
            if (findUnescaped(filter, valueStartIndex, filterEndIndex) == -1) {
                filterType = TYPE_EQUALITY;
            } else if (filter[valueStartIndex] == '*' && valueStartIndex == (filterEndIndex - 1)) {
                filterType = TYPE_PRESENT;
                buffer.writeOctetString(filterType,
                        filter,
                        filterStartIndex,
                        filterTypeEndIndex - filterStartIndex);
                return;
            } else {
                writeSubstringFilter(buffer,
                        filter,
                        filterStartIndex,
                        filterTypeEndIndex,
                        valueStartIndex,
                        filterEndIndex);
                return;
            }
        }

        if (filterType == TYPE_EXTENSIBLE_MATCH) {
            writeExtensibleMatchFilter(buffer,
                    filter,
                    filterStartIndex,
                    filterTypeEndIndex,
                    valueStartIndex,
                    filterEndIndex);
        } else {
            buffer.beginSequence(filterType)
                    .writeOctetString(filter,
                            filterStartIndex,
                            filterTypeEndIndex - filterStartIndex);
            byte[] filterValue = unescapeFilterValue(filter, valueStartIndex, filterEndIndex);
            if (filterValue == null) {
                buffer.writeOctetString(filter, valueStartIndex, filterEndIndex - valueStartIndex);
            } else {
                buffer.writeOctetString(filterValue, 0, filterValue.length);
            }
            buffer.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