1 package org.vaadin.aceeditor.client;
2
3 import java.util.Map;
4 import java.util.Map.Entry;
5 import java.util.Set;
6
7 public class Util {
8
9
10 public static int cursorPosFromLineCol(String text, int line, int col,
11 int firstLineNum) {
12 return cursorPosFromLineCol(text.split("\n", -1), line, col,
13 firstLineNum);
14 }
15
16 public static int cursorPosFromLineCol(String[] lines, int line, int col,
17 int firstLineNum) {
18 line -= firstLineNum;
19 int pos = 0;
20 for (int currLine = 0; currLine < lines.length; ++currLine) {
21 if (currLine < line) {
22 pos += lines[currLine].length() + 1;
23 } else if (currLine == line) {
24 pos += col;
25 break;
26 }
27 }
28 return pos;
29 }
30
31 public static int cursorPosFromLineCol(int[] lineLengths, int line, int col,
32 int firstLineNum) {
33 line -= firstLineNum;
34 int pos = 0;
35 for (int currLine = 0; currLine < lineLengths.length; ++currLine) {
36 if (currLine < line) {
37 pos += lineLengths[currLine] + 1;
38 } else if (currLine == line) {
39 pos += col;
40 break;
41 }
42 }
43 return pos;
44 }
45
46
47 public static int[] lineColFromCursorPos(String text, int pos,
48 int firstLineNum) {
49 return lineColFromCursorPos(text.split("\n", -1), pos, firstLineNum);
50 }
51
52 public static int[] lineColFromCursorPos(String[] lines, int pos,
53 int firstLineNum) {
54 int lineno = 0;
55 int col = pos;
56 for (String li : lines) {
57 if (col <= li.length()) {
58 break;
59 }
60 lineno += 1;
61 col -= (li.length() + 1);
62 }
63 lineno += firstLineNum;
64
65 return new int[] { lineno, col };
66 }
67
68 public static int[] lineColFromCursorPos(int[] lineLengths, int pos,
69 int firstLineNum) {
70 int lineno = 0;
71 int col = pos;
72 for (int len : lineLengths) {
73 if (col <= len) {
74 break;
75 }
76 lineno += 1;
77 col -= (len + 1);
78 }
79 lineno += firstLineNum;
80
81 return new int[] { lineno, col };
82 }
83
84 public static int count(char c, String text) {
85 int n = 0;
86 int from = 0;
87 while (true) {
88 int index = text.indexOf(c, from);
89 if (index == -1) {
90 return n;
91 } else {
92 ++n;
93 from = index + 1;
94 }
95 }
96 }
97
98 public static int startColOfCursorLine(String text, int cursor) {
99 int i = cursor;
100 while (i > 0) {
101 char c = text.charAt(i - 1);
102 if (c == '\n') {
103 break;
104 }
105 --i;
106 }
107 return i;
108 }
109
110 public static String indentationStringOfCursorLine(String text, int cursor) {
111 int lineStart = startColOfCursorLine(text, cursor);
112 int firstCharAt = lineStart;
113 while (firstCharAt < text.length()) {
114
115 if (text.charAt(firstCharAt) != ' ') {
116 break;
117 }
118 ++firstCharAt;
119 }
120 return text.substring(lineStart, firstCharAt);
121 }
122
123
124 public static String join(String[] lines) {
125 StringBuilder sb = new StringBuilder();
126 boolean first = true;
127 for (String li : lines) {
128 if (first) {
129 sb.append(li);
130 first = false;
131 } else {
132 sb.append("\n").append(li);
133 }
134
135 }
136 return sb.toString();
137 }
138
139 public static <K,V> boolean sameMaps(
140 Map<K, V> map1,
141 Map<K, V> map2) {
142 if (map1.size() != map2.size()) {
143 return false;
144 }
145 for(Entry<K, V> e : map1.entrySet()) {
146 V m1 = e.getValue();
147 V m2 = map2.get(e.getKey());
148 if (!m1.equals(m2)) {
149 return false;
150 }
151 }
152 return true;
153 }
154
155 public static <V> boolean sameSets(Set<V> set1, Set<V> set2) {
156 if (set1.size()!=set2.size()) {
157 return false;
158 }
159 for (V v : set1) {
160 if (!set2.contains(v)) {
161 return false;
162 }
163 }
164 return true;
165 }
166
167 public static String replaceContents(AceRange ofThis, String inText, String withThis) {
168 String[] lines = inText.split("\n", -1);
169 int start = Util.cursorPosFromLineCol(lines, ofThis.getStartRow(), ofThis.getStartCol(), 0);
170 int end = Util.cursorPosFromLineCol(lines, ofThis.getEndRow(), ofThis.getEndCol(), 0);
171 return inText.substring(0, start) + withThis + inText.substring(end);
172 }
173
174 }