Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f0237323542209e741d829a2f565205ff8d96cd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core.sourcelookup;

import java.util.Comparator;

/**
 * Comparator for source locator mementos. Ignores whitespace differences.
 *
 * @since 3.0
 */
public class SourceLocatorMementoComparator implements Comparator<String> {

	@Override
	public int compare(String o1, String o2) {
		String m1 = o1;
		String m2 = o2;
		int i1 = 0, i2 = 0;
		while (i1 < m1.length()) {
			i1 = skipWhitespace(m1, i1);
			i2 = skipWhitespace(m2, i2);
			if (i1 < m1.length() && i2 < m2.length()) {
				if (m1.charAt(i1) != m2.charAt(i2)) {
					return -1;
				}
				i1++;
				i2++;
			} else {
				if (i2 < m2.length()) {
					return -1;
				}
				return 0;
			}
		}
		return 0;
	}

	private int skipWhitespace(String string, int offset) {
		int off = offset;
		while (off < string.length() && Character.isWhitespace(string.charAt(off))) {
			off++;
		}
		return off;
	}
}

Back to the top