private int substitute()

in gflog-core/src/main/java/com/epam/deltix/gflog/core/util/StringSubstitution.java [42:103]


    private int substitute(final String source, int index, final StringBuilder target) {
        final int end = source.length();
        final int start = index;

        index += 2;

        while (index < end) {
            char c = source.charAt(index);

            if (c == '}') {
                final String key = source.substring(start + 2, index);
                final String value = lookup(key);

                if (value == null) {
                    target.append("${").append(key).append(":#UNRESOLVED#").append("}");
                } else {
                    target.append(value);
                }

                return index + 1;
            }

            if (c == ':' && index + 1 < end && source.charAt(index + 1) == '-') {
                final String key = source.substring(start + 2, index);
                final String value = lookup(key);

                final int length = target.length();
                final int mid = index + 2;

                index = mid;

                while (index < end) {
                    c = source.charAt(index);

                    if (c == '}') {
                        if (value != null) {
                            target.setLength(length);
                            target.append(value);
                        }

                        return index + 1;
                    }

                    if (c == '$' && index + 1 < end && source.charAt(index + 1) == '{') {
                        index = substitute(source, index, target);
                        continue;
                    }

                    target.append(c);
                    index++;
                }

                target.insert(length, source, start, mid);
                return end;
            }

            index++;
        }

        target.append(source, start, end);
        return index;
    }