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	final static List<Character> KEYWORDS = Arrays.asList('*', '_', '~', '`');
 39	final static List<Character> NO_SUB_PARSING_KEYWORDS = Arrays.asList('`');
 40	final static boolean ALLOW_EMPTY = false;
 41
 42	public static List<Style> parse(CharSequence text) {
 43		return parse(text, 0, text.length() - 1);
 44	}
 45
 46	public static List<Style> parse(CharSequence text, int start, int end) {
 47		List<Style> styles = new ArrayList<>();
 48		for (int i = start; i <= end; ++i) {
 49			char c = text.charAt(i);
 50			if (KEYWORDS.contains(c) && precededByWhiteSpace(text, i, start) && !followedByWhitespace(text, i, end)) {
 51				int to = seekEnd(text, c, i + 1, end);
 52				if (to != -1 && (to != i + 1 || ALLOW_EMPTY)) {
 53					styles.add(new Style(c, i, to));
 54					if (!NO_SUB_PARSING_KEYWORDS.contains(c)) {
 55						styles.addAll(parse(text, i + 1, to - 1));
 56					}
 57					i = to;
 58				}
 59			}
 60		}
 61		return styles;
 62	}
 63
 64	private static boolean precededByWhiteSpace(CharSequence text, int index, int start) {
 65		return index == start || Character.isWhitespace(text.charAt(index - 1));
 66	}
 67
 68	private static boolean followedByWhitespace(CharSequence text, int index, int end) {
 69		return index >= end || Character.isWhitespace(text.charAt(index + 1));
 70	}
 71
 72	private static int seekEnd(CharSequence text, char needle, int start, int end) {
 73		for (int i = start; i <= end; ++i) {
 74			char c = text.charAt(i);
 75			if (c == needle) {
 76				return i;
 77			} else if (c == '\n') {
 78				return -1;
 79			}
 80		}
 81		return -1;
 82	}
 83
 84	public static class Style {
 85
 86		private final char c;
 87		private final int start;
 88		private final int end;
 89
 90		public Style(char c, int start, int end) {
 91			this.c = c;
 92			this.start = start;
 93			this.end = end;
 94		}
 95
 96		public char getCharacter() {
 97			return c;
 98		}
 99
100		public int getStart() {
101			return start;
102		}
103
104		public int getEnd() {
105			return end;
106		}
107	}
108}