1/*
  2 * Copyright (c) 2017, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.utils;
 31
 32import java.util.ArrayList;
 33import java.util.Arrays;
 34import java.util.List;
 35
 36public class ImStyleParser {
 37
 38    private static final List<Character> KEYWORDS = Arrays.asList('*', '_', '~', '`');
 39    private static final List<Character> NO_SUB_PARSING_KEYWORDS = List.of('`');
 40    private static final List<Character> BLOCK_KEYWORDS = List.of('`');
 41    private static final boolean ALLOW_EMPTY = false;
 42    private static final boolean PARSE_HIGHER_ORDER_END = true;
 43
 44    public static List<Style> parse(CharSequence text) {
 45        return parse(text, 0, text.length() - 1);
 46    }
 47
 48    public static List<Style> parse(CharSequence text, int start, int end) {
 49        List<Style> styles = new ArrayList<>();
 50        for (int i = start; i <= end; ++i) {
 51            char c = text.charAt(i);
 52            if (KEYWORDS.contains(c)
 53                    && precededByWhiteSpace(text, i, start)
 54                    && !followedByWhitespace(text, i, end)) {
 55                if (BLOCK_KEYWORDS.contains(c) && isCharRepeatedTwoTimes(text, c, i + 1, end)) {
 56                    int to = seekEndBlock(text, c, i + 3, end);
 57                    if (to != -1 && (to != i + 5 || ALLOW_EMPTY)) {
 58                        String keyword = String.valueOf(c) + c + c;
 59                        styles.add(new Style(keyword, i, to));
 60                        i = to;
 61                        continue;
 62                    }
 63                    continue;
 64                }
 65                int to = seekEnd(text, c, i + 1, end);
 66                if (to != -1 && (to != i + 1 || ALLOW_EMPTY)) {
 67                    styles.add(new Style(c, i, to));
 68                    if (!NO_SUB_PARSING_KEYWORDS.contains(c)) {
 69                        styles.addAll(parse(text, i + 1, to - 1));
 70                    }
 71                    i = to;
 72                }
 73            }
 74        }
 75        return styles;
 76    }
 77
 78    private static boolean isCharRepeatedTwoTimes(CharSequence text, char c, int index, int end) {
 79        return index + 1 <= end && text.charAt(index) == c && text.charAt(index + 1) == c;
 80    }
 81
 82    private static boolean precededByWhiteSpace(CharSequence text, int index, int start) {
 83        return index == start || Character.isWhitespace(text.charAt(index - 1));
 84    }
 85
 86    private static boolean followedByWhitespace(CharSequence text, int index, int end) {
 87        return index >= end || Character.isWhitespace(text.charAt(index + 1));
 88    }
 89
 90    private static int seekEnd(CharSequence text, char needle, int start, int end) {
 91        for (int i = start; i <= end; ++i) {
 92            char c = text.charAt(i);
 93            if (c == needle && !Character.isWhitespace(text.charAt(i - 1))) {
 94                if (!PARSE_HIGHER_ORDER_END || followedByWhitespace(text, i, end)) {
 95                    return i;
 96                } else {
 97                    int higherOrder =
 98                            seekHigherOrderEndWithoutNewBeginning(text, needle, i + 1, end);
 99                    if (higherOrder != -1) {
100                        return higherOrder;
101                    }
102                    return i;
103                }
104            } else if (c == '\n') {
105                return -1;
106            }
107        }
108        return -1;
109    }
110
111    private static int seekHigherOrderEndWithoutNewBeginning(
112            CharSequence text, char needle, int start, int end) {
113        for (int i = start; i <= end; ++i) {
114            char c = text.charAt(i);
115            if (c == needle
116                    && precededByWhiteSpace(text, i, start)
117                    && !followedByWhitespace(text, i, end)) {
118                return -1; // new beginning
119            } else if (c == needle
120                    && !Character.isWhitespace(text.charAt(i - 1))
121                    && followedByWhitespace(text, i, end)) {
122                return i;
123            } else if (c == '\n') {
124                return -1;
125            }
126        }
127        return -1;
128    }
129
130    private static int seekEndBlock(CharSequence text, char needle, int start, int end) {
131        var foundNewline = false;
132        for (int i = start; i <= end; ++i) {
133            char c = text.charAt(i);
134            if (c == '\n') foundNewline = true;
135            if (foundNewline && c == needle && isCharRepeatedTwoTimes(text, needle, i + 1, end)) {
136                return i + 2;
137            }
138        }
139        return -1;
140    }
141
142    public static class Style {
143
144        private final String keyword;
145        private final int start;
146        private final int end;
147
148        public Style(char character, int start, int end) {
149            this(String.valueOf(character), start, end);
150        }
151
152        public Style(String keyword, int start, int end) {
153            this.keyword = keyword;
154            this.start = start;
155            this.end = end;
156        }
157
158        public String getKeyword() {
159            return keyword;
160        }
161
162        public int getStart() {
163            return start;
164        }
165
166        public int getEnd() {
167            return end;
168        }
169    }
170}