ImStyleParser.java

  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 final static List<Character> KEYWORDS = Arrays.asList('*', '_', '~', '`');
 39	private final static List<Character> NO_SUB_PARSING_KEYWORDS = Arrays.asList('`');
 40	private final static List<Character> BLOCK_KEYWORDS = Arrays.asList('`');
 41	private final static boolean ALLOW_EMPTY = false;
 42
 43	public static List<Style> parse(CharSequence text) {
 44		return parse(text, 0, text.length() - 1);
 45	}
 46
 47	public static List<Style> parse(CharSequence text, int start, int end) {
 48		List<Style> styles = new ArrayList<>();
 49		for (int i = start; i <= end; ++i) {
 50			char c = text.charAt(i);
 51			if (KEYWORDS.contains(c) && precededByWhiteSpace(text, i, start) && !followedByWhitespace(text, i, end)) {
 52				if (BLOCK_KEYWORDS.contains(c) && isCharRepeatedTwoTimes(text, c, i + 1, end)) {
 53					int to = seekEndBlock(text, c, i + 3, end);
 54					if (to != -1 && (to != i + 5 || ALLOW_EMPTY)) {
 55						String keyword = String.valueOf(c) + String.valueOf(c) + String.valueOf(c);
 56						styles.add(new Style(keyword, i, to));
 57						i = to;
 58						continue;
 59					}
 60				}
 61				int to = seekEnd(text, c, i + 1, end);
 62				if (to != -1 && (to != i + 1 || ALLOW_EMPTY)) {
 63					styles.add(new Style(c, i, to));
 64					if (!NO_SUB_PARSING_KEYWORDS.contains(c)) {
 65						styles.addAll(parse(text, i + 1, to - 1));
 66					}
 67					i = to;
 68				}
 69			}
 70		}
 71		return styles;
 72	}
 73
 74	private static boolean isCharRepeatedTwoTimes(CharSequence text, char c, int index, int end) {
 75		return index + 1 <= end && text.charAt(index) == c && text.charAt(index+1) == c;
 76	}
 77
 78	private static boolean precededByWhiteSpace(CharSequence text, int index, int start) {
 79		return index == start || Character.isWhitespace(text.charAt(index - 1));
 80	}
 81
 82	private static boolean followedByWhitespace(CharSequence text, int index, int end) {
 83		return index >= end || Character.isWhitespace(text.charAt(index + 1));
 84	}
 85
 86	private static int seekEnd(CharSequence text, char needle, int start, int end) {
 87		for (int i = start; i <= end; ++i) {
 88			char c = text.charAt(i);
 89			if (c == needle && !Character.isWhitespace(text.charAt(i - 1))) {
 90				return i;
 91			} else if (c == '\n') {
 92				return -1;
 93			}
 94		}
 95		return -1;
 96	}
 97
 98	private static int seekEndBlock(CharSequence text, char needle, int start, int end) {
 99		for (int i = start; i <= end; ++i) {
100			char c = text.charAt(i);
101			if (c == needle && isCharRepeatedTwoTimes(text, needle, i + 1, end)) {
102				return i + 2;
103			}
104		}
105		return -1;
106	}
107
108	public static class Style {
109
110		private final String keyword;
111		private final int start;
112		private final int end;
113
114		public Style(char character, int start, int end) {
115			this(String.valueOf(character), start, end);
116		}
117
118		public Style(String keyword, int start, int end) {
119			this.keyword = keyword;
120			this.start = start;
121			this.end = end;
122		}
123
124		public String getKeyword() {
125			return keyword;
126		}
127
128		public int getStart() {
129			return start;
130		}
131
132		public int getEnd() {
133			return end;
134		}
135	}
136}