Skip to content
Snippets Groups Projects
Commit 2c24c213 authored by pat's avatar pat
Browse files

fix a previously unfixed bug

and all other bugs, even the ones I don't even know!
parent b7349a60
No related branches found
No related tags found
No related merge requests found
...@@ -36,4 +36,10 @@ public interface ModifiableString extends CharSequence { ...@@ -36,4 +36,10 @@ public interface ModifiableString extends CharSequence {
return toString().getBytes(cs); return toString().getBytes(cs);
} }
@Override
int hashCode();
@Override
boolean equals(Object obj);
} }
...@@ -3,17 +3,12 @@ package de.hechler.patrick.utils.cc.file; ...@@ -3,17 +3,12 @@ package de.hechler.patrick.utils.cc.file;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Objects;
public class CCPath implements CCFile { public record CCPath(Path path) implements CCFile {
private final Path path; public CCPath {
Objects.requireNonNull(path, "path");
public CCPath(Path path) {
this.path = path;
}
public Path path() {
return path;
} }
@Override @Override
...@@ -26,38 +21,9 @@ public class CCPath implements CCFile { ...@@ -26,38 +21,9 @@ public class CCPath implements CCFile {
return Files.readAllBytes(path); return Files.readAllBytes(path);
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CCPath other = (CCPath) obj;
if (path == null) {
if (other.path != null)
return false;
} else if (!path.equals(other.path))
return false;
return true;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); return path.toString();
builder.append("CCPath [path=");
builder.append(path);
builder.append("]");
return builder.toString();
} }
} }
...@@ -3,23 +3,13 @@ package de.hechler.patrick.utils.cc.file; ...@@ -3,23 +3,13 @@ package de.hechler.patrick.utils.cc.file;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Objects;
public class CCSrcDstPath implements CCFile { public record CCSrcDstPath(Path src, Path dst) implements CCFile {
private final Path src; public CCSrcDstPath {
private final Path dst; Objects.requireNonNull(src, "source path");
Objects.requireNonNull(dst, "destination path");
public CCSrcDstPath(Path src, Path dst) {
this.src = src;
this.dst = dst;
}
public Path src() {
return src;
}
public Path dst() {
return dst;
} }
@Override @Override
...@@ -33,46 +23,13 @@ public class CCSrcDstPath implements CCFile { ...@@ -33,46 +23,13 @@ public class CCSrcDstPath implements CCFile {
Files.write(dst, content); Files.write(dst, content);
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dst == null) ? 0 : dst.hashCode());
result = prime * result + ((src == null) ? 0 : src.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CCSrcDstPath other = (CCSrcDstPath) obj;
if (dst == null) {
if (other.dst != null)
return false;
} else if (!dst.equals(other.dst))
return false;
if (src == null) {
if (other.src != null)
return false;
} else if (!src.equals(other.src))
return false;
return true;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder b = new StringBuilder();
builder.append("CCSrcDstPath [src="); b.append(src);
builder.append(src); b.append(" ==> ");
builder.append(", dst="); b.append(dst);
builder.append(dst); return b.toString();
builder.append("]");
return builder.toString();
} }
} }
...@@ -3,6 +3,7 @@ package de.hechler.patrick.utils.cc.file; ...@@ -3,6 +3,7 @@ package de.hechler.patrick.utils.cc.file;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Objects;
public class CCStream implements CCFile { public class CCStream implements CCFile {
...@@ -10,8 +11,8 @@ public class CCStream implements CCFile { ...@@ -10,8 +11,8 @@ public class CCStream implements CCFile {
private final OutputStream out; private final OutputStream out;
public CCStream(InputStream in, OutputStream out) { public CCStream(InputStream in, OutputStream out) {
this.in = in; this.in = Objects.requireNonNull(in, "input stream");
this.out = out; this.out = Objects.requireNonNull(out, "output stream");
} }
@Override @Override
...@@ -46,15 +47,9 @@ public class CCStream implements CCFile { ...@@ -46,15 +47,9 @@ public class CCStream implements CCFile {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
CCStream other = (CCStream) obj; CCStream other = (CCStream) obj;
if (in == null) { if (!in.equals(other.in))
if (other.in != null)
return false; return false;
} else if (!in.equals(other.in)) if (!out.equals(other.out))
return false;
if (out == null) {
if (other.out != null)
return false;
} else if (!out.equals(other.out))
return false; return false;
return true; return true;
} }
......
package de.hechler.patrick.utils.cc.ms;
import java.nio.charset.Charset;
import java.util.Arrays;
import de.hechler.patrick.utils.cc.ModifiableString;
public class DuplicateModifiableString implements ModifiableString {
private final ModifiableString a;
private final ModifiableString b;
public DuplicateModifiableString() {
this(new ModStringBuilder(), new FastStringList());
}
public DuplicateModifiableString(ModifiableString b) {
this(new ModStringBuilder(), b);
}
public DuplicateModifiableString(ModifiableString a, ModifiableString b) {
checkEquals(a, b);
checkEquals(a.hashCode(), b.hashCode());
this.a = a;
this.b = b;
}
private void checkEquals() {
checkEquals(a, b);
}
private static int checkEquals(int a, int b) {
if (a != b) {
throw new AssertionError();
}
return a;
}
private static char checkEquals(char a, char b) {
if (a != b) {
throw new AssertionError();
}
return a;
}
private static boolean checkEquals(boolean a, boolean b) {
if (a != b) {
throw new AssertionError();
}
return a;
}
private static <T> T checkEquals(T a, T b) {
if (!a.equals(b) || a == b) {
throw new AssertionError();
}
return a;
}
private static byte[] checkEquals(byte[] a, byte[] b) {
if (!Arrays.equals(a, b) || a == b) {
throw new AssertionError();
}
return a;
}
@Override
public int length() {
return checkEquals(a.length(), b.length());
}
@Override
public char charAt(int index) {
char c = a.charAt(index);
char c2 = b.charAt(index);
return checkEquals(c, c2);
}
@Override
public CharSequence subSequence(int start, int end) {
CharSequence cs = a.subSequence(start, end);
CharSequence cs2 = b.subSequence(start, end);
return checkEquals(cs, cs2);
}
@Override
public int indexOf(char c, int off) {
int i = a.indexOf(c, off);
int i2 = b.indexOf(c, off);
return checkEquals(i, i2);
}
@Override
public int lastIndexOf(char c, int off) {
int i = a.lastIndexOf(c, off);
int i2 = b.lastIndexOf(c, off);
return checkEquals(i, i2);
}
@Override
public void replace(int start, int end, String str) {
a.replace(start, end, str);
b.replace(start, end, str);
checkEquals();
}
@Override
public void replace(int start, int end, char[] arr, int off, int len) {
a.replace(start, end, arr, off, len);
b.replace(start, end, arr, off, len);
checkEquals();
}
@Override
public void insert(int index, String str) {
a.insert(index, str);
b.insert(index, str);
checkEquals();
}
@Override
public void insert(int index, char[] arr, int off, int len) {
a.insert(index, arr, off, len);
b.insert(index, arr, off, len);
checkEquals();
}
@Override
public void append(String str) {
a.append(str);
b.append(str);
checkEquals();
}
@Override
public void append(char[] arr, int off, int len) {
a.append(arr, off, len);
b.append(arr, off, len);
checkEquals();
}
@Override
public String toString() {
return checkEquals(a.toString(), b.toString());
}
@Override
public byte[] getBytes(Charset cs) {
return checkEquals(a.getBytes(cs), b.getBytes(cs));
}
@Override
public int hashCode() {
return checkEquals(a.hashCode(), b.hashCode());
}
@Override
public boolean equals(Object obj) {
return checkEquals(a.equals(obj), b.equals(obj));
}
}
package de.hechler.patrick.utils.cc.ms; package de.hechler.patrick.utils.cc.ms;
import java.io.ByteArrayOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
...@@ -12,21 +8,27 @@ import java.util.Arrays; ...@@ -12,21 +8,27 @@ import java.util.Arrays;
import de.hechler.patrick.utils.cc.ModifiableString; import de.hechler.patrick.utils.cc.ModifiableString;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public final class FastStringList implements ModifiableString { public class FastStringList implements ModifiableString {
private static final int ARR_MAX_SIZE = 2048; private static final int ARR_MAX_SIZE = 2048;
private static final int ARR_PREF_SIZE = ARR_MAX_SIZE - (ARR_MAX_SIZE / 8);
private static final int ARR_MIN_SIZE = ARR_MAX_SIZE / 2; private static final int ARR_MIN_SIZE = ARR_MAX_SIZE / 2;
private static final int ARR_PREF_SIZE = (ARR_MAX_SIZE + ARR_MIN_SIZE) / 2;
// length/offset is only used/maintained in the user object // length/offset is only used/maintained in the user object
private int length; protected int length;
private int offset; protected int offset;
private FastStringList prev; protected FastStringList prev;
private int dataSize; protected int dataSize;
private char[] data; protected char[] data;
private FastStringList next; protected FastStringList next;
// nextData is only used/maintained in the userObject // nextData is only used/maintained in the userObject
private char[] nextData; protected char[] nextData;
public FastStringList(boolean init) {
if (init) {
throw new AssertionError();
}
}
public FastStringList() { public FastStringList() {
data = new char[ARR_MAX_SIZE]; data = new char[ARR_MAX_SIZE];
...@@ -107,7 +109,7 @@ public final class FastStringList implements ModifiableString { ...@@ -107,7 +109,7 @@ public final class FastStringList implements ModifiableString {
} }
} }
private void dataSize(int nml) { protected void dataSize(int nml) {
if (nml > ARR_MAX_SIZE) { if (nml > ARR_MAX_SIZE) {
System.err.println("BREAK!"); System.err.println("BREAK!");
// throw new AssertionError(nml); // throw new AssertionError(nml);
...@@ -491,8 +493,8 @@ public final class FastStringList implements ModifiableString { ...@@ -491,8 +493,8 @@ public final class FastStringList implements ModifiableString {
off += ml; off += ml;
sl = sl.next; sl = sl.next;
char[] nd = sl.data; char[] nd = sl.data;
System.arraycopy(d, ml, nd, 0, ei);
ml = sl.dataSize; ml = sl.dataSize;
System.arraycopy(d, sl.dataSize, nd, 0, ei);
d = nd; d = nd;
} }
// : insert the new data // : insert the new data
...@@ -593,8 +595,13 @@ public final class FastStringList implements ModifiableString { ...@@ -593,8 +595,13 @@ public final class FastStringList implements ModifiableString {
System.arraycopy(sl.data, ei, rsd, offset + si, ml - ei); System.arraycopy(sl.data, ei, rsd, offset + si, ml - ei);
System.arraycopy(pd, npml, rsd, 0, offset); System.arraycopy(pd, npml, rsd, 0, offset);
} else { } else {
System.arraycopy(sl.data, ei, rsd, si, ml - ei); int cpy = ml - ei;
System.arraycopy(pd, npml, rsd, 0, pml - npml); System.arraycopy(sl.data, ei, rsd, si, cpy);
int pos = si + cpy;
cpy = ARR_MIN_SIZE - cpy - si;
System.arraycopy(pd, 0, rsd, pos, cpy);
System.arraycopy(pd, cpy, pd, 0, npml);
assert pml - cpy == npml;
} }
removeStart.dataSize(ARR_MIN_SIZE); removeStart.dataSize(ARR_MIN_SIZE);
p.dataSize(npml); p.dataSize(npml);
...@@ -747,6 +754,90 @@ public final class FastStringList implements ModifiableString { ...@@ -747,6 +754,90 @@ public final class FastStringList implements ModifiableString {
} }
} }
@Override
public int hashCode() {
FastStringList sl = this;
while (sl.prev != null)
sl = sl.prev;
int result = 0;
final int prime = 31;
for (; sl != null; sl = sl.next) {
int ml = sl.dataSize;
char[] d = sl.data;
for (int i = 0; i < ml; i++) {
result = result * prime + d[i];
}
}
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ModifiableString ms)) {
return false;
}
if (!(obj instanceof FastStringList osl)) {
if (ms.length() != length) {
return false;
}
FastStringList sl = this;
while (sl.prev != null)
sl = sl.prev;
for (int off = 0, noff; sl != null; sl = sl.next, off = noff) {
int ml = sl.dataSize;
noff = off + ml;
CharSequence cs = ms.subSequence(off, noff);
if (!cs.toString().equals(new String(sl.data, 0, ml))) {
return false;
}
}
return true;
}
if (length != osl.length) {
return false;
}
FastStringList sl = this;
while (sl.prev != null)
sl = sl.prev;
while (osl.prev != null)
osl = osl.prev;
int i = 0, oi = 0;
int ml = sl.dataSize;
int oml = osl.dataSize;
while (true) {
if (i == ml) {
sl = sl.next;
if (sl != null) {
ml = sl.dataSize;
i = 0;
}
}
if (oi == oml) {
osl = osl.next;
if (osl != null) {
oml = osl.dataSize;
oi = 0;
}
}
if (sl == osl) {
if (sl != null) {
throw new AssertionError();
}
return true;
}
int len = Math.min(ml - i, oml - oi);
int mm = Arrays.mismatch(sl.data, i, i + len, osl.data, oi, oi + len);
if (mm != -1) {
return false;
}
i += len;
oi += len;
}
}
@Override @Override
public String toString() { public String toString() {
StringBuilder result = new StringBuilder(length); StringBuilder result = new StringBuilder(length);
......
...@@ -88,6 +88,19 @@ public class ModStringBuilder implements ModifiableString { ...@@ -88,6 +88,19 @@ public class ModStringBuilder implements ModifiableString {
this.sb.append(arr, off, len); this.sb.append(arr, off, len);
} }
@Override
public int hashCode() {
return sb.toString().hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ModifiableString)) {
return false;
}
return sb.toString().equals(obj.toString());
}
@Override @Override
public String toString() { public String toString() {
return this.sb.toString(); return this.sb.toString();
......
...@@ -53,7 +53,7 @@ public class SqlCorrector extends MassCarCoder { ...@@ -53,7 +53,7 @@ public class SqlCorrector extends MassCarCoder {
cc.insert("ì\\n"); cc.insert("ì\\n");
} }
if (change || !(p instanceof CCPath)) { if (change || !(p instanceof CCPath)) {
cc.save(p); // cc.save(p);
} }
if (!change) { if (!change) {
System.out.println(" nothing changed"); System.out.println(" nothing changed");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment