Skip to content

Commit

Permalink
Generic resize
Browse files Browse the repository at this point in the history
Example update
  • Loading branch information
jdunkerley committed Apr 19, 2022
1 parent c33bf4c commit 4b06815
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ new path = case path of

import Standard.Examples

example_read_bytes = Examples.csv.read_bytes
example_read_bytes = File.read_bytes Examples.csv
read_bytes : (Text | File) -> Vector.Vector ! File_Error
read_bytes path =
file = case path of
Expand Down
27 changes: 11 additions & 16 deletions std-bits/base/src/main/java/org/enso/base/Text_Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ibm.icu.text.Normalizer2;
import com.ibm.icu.text.StringSearch;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand All @@ -19,6 +20,9 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.regex.Pattern;
import org.enso.base.text.CaseFoldedString;
import org.enso.base.text.CaseFoldedString.Grapheme;
Expand Down Expand Up @@ -75,12 +79,12 @@ public static String drop_first(String string, int from) {
return string.substring(from);
}

private static ByteBuffer resize(ByteBuffer old) {
private static <T extends Buffer> T resize(T old, IntFunction<T> allocate, BiConsumer<T, T> put) {
int n = old.capacity();
int new_n = 2*n + 1;
ByteBuffer o = ByteBuffer.allocate(new_n);
T o = allocate.apply(new_n);
old.flip();
o.put(old);
put.accept(o, old);
return o;
}

Expand Down Expand Up @@ -113,7 +117,7 @@ public static ResultWithWarnings<byte[]> get_bytes(String str, Charset charset)
int position = in.position();

if (out.remaining() < encoder.replacement().length) {
out = resize(out);
out = resize(out, ByteBuffer::allocate, ByteBuffer::put);
}
out.put(encoder.replacement());
in.position(in.position() + cr.length());
Expand All @@ -130,7 +134,7 @@ public static ResultWithWarnings<byte[]> get_bytes(String str, Charset charset)
encoder.flush(out);
break;
} else if (cr.isOverflow()) {
out = resize(out);
out = resize(out, ByteBuffer::allocate, ByteBuffer::put);
}
}

Expand Down Expand Up @@ -238,15 +242,6 @@ public static String from_codepoints(int[] codepoints) {
return new String(codepoints, 0, codepoints.length);
}

private static CharBuffer resize(CharBuffer old) {
int n = old.capacity();
int new_n = 2*n + 1;
CharBuffer o = CharBuffer.allocate(new_n);
old.flip();
o.put(old);
return o;
}

/**
* Converts an array of encoded bytes into a string.
*
Expand Down Expand Up @@ -276,7 +271,7 @@ public static ResultWithWarnings<String> from_bytes(byte[] bytes, Charset charse
int position = in.position();

if (out.remaining() < INVALID_CHARACTER.length()) {
out = resize(out);
out = resize(out, CharBuffer::allocate, CharBuffer::put);
}
out.put(INVALID_CHARACTER);
in.position(in.position() + cr.length());
Expand All @@ -293,7 +288,7 @@ public static ResultWithWarnings<String> from_bytes(byte[] bytes, Charset charse
decoder.flush(out);
break;
} else if (cr.isOverflow()) {
out = resize(out);
out = resize(out, CharBuffer::allocate, CharBuffer::put);
}
}

Expand Down

0 comments on commit 4b06815

Please sign in to comment.