001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.parboiled.buffers;
018
019import org.parboiled.support.Chars;
020import org.parboiled.support.Chars;
021
022public final class InputBufferUtils {
023
024    private InputBufferUtils() {}
025
026    /**
027     * Collects the actual input text the input buffer provides into a String.
028     * This is especially useful for IndentDedentInputBuffers created by "transformIndents".
029     * @param buf the input buffer to collect from
030     * @return a string containing the content of the given input buffer
031     */
032    public static String collectContent(InputBuffer buf) {
033        StringBuilder sb = new StringBuilder();
034        int ix = 0;
035
036        loop:
037        while (true) {
038            char c = buf.charAt(ix++);
039            switch (c) {
040                case Chars.INDENT:
041                    sb.append('\u00bb'); // right pointed double angle quotation mark
042                    break;
043                case Chars.DEDENT:
044                    sb.append('\u00ab'); // left pointed double angle quotation mark
045                    break;
046                case Chars.EOI:
047                    break loop;
048                default:
049                    sb.append(c);
050            }
051        }
052        return sb.toString();
053    }
054}