Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a "cap" (capacity) field to WriteBuffer. Converted AppendBuffer…

… to use

"size" instead of "pos" as the write marker, which works better with most
use-cases.
  • Loading branch information...
commit 5990010369ba0e9ae54b1da4522c975d44640855 1 parent 3f94ff8
mmuller@google.com authored
View
8 lib/crack/ascii.crk
@@ -54,8 +54,10 @@ int parseInt(String rep) {
return val * sign;
}
-String _changeCase(String text, byte lowerBound, byte upperBound, byte newOffset,
- uint num)
+String _changeCase(String text, byte lowerBound, byte upperBound,
+ byte newOffset,
+ uint num
+ )
{
oldBuf := text.buffer;
ManagedBuffer newVal = {text.size};
@@ -72,6 +74,7 @@ String _changeCase(String text, byte lowerBound, byte upperBound, byte newOffset
}
}
+ newVal.size = text.size;
return String(newVal, true);
}
@@ -183,7 +186,6 @@ String escape(Buffer data, byte min, byte max) {
}
}
- buf.size = buf.pos;
return String(buf, true);
}
View
1  lib/crack/extgen.crk
@@ -341,7 +341,6 @@ class SpecializedType : TypeImpl {
result.append(ch);
}
- result.size = result.pos;
return String(result, true);
}
View
4 lib/crack/ffmt/ini.crk
@@ -343,7 +343,7 @@ uint IniParser_en_main = 13;
else if (_tempval == 8) { // FROM_STATE_ACTION_SWITCH
# line 94 "opt/ini.rl" # end of line directive
- appendBuf.pos = 0;
+ appendBuf.size = 0;
okp = p+1;
// ACTION
}
@@ -403,7 +403,7 @@ uint IniParser_en_main = 13;
if (p > okp)
appendBuf.extend(data + uintz(okp), p - okp);
- value = String(appendBuf, 0, appendBuf.pos);
+ value = String(appendBuf, 0, appendBuf.size);
_sectionMap[key] = value;
okp = p+1;
View
2  lib/crack/fs.crk
@@ -236,7 +236,7 @@ class _CWD : Path {
CString __getcwd() {
ManagedBuffer buf = {PATH_MAX};
- if (getcwd(buf.buffer, buf.size) is null) {
+ if (getcwd(buf.buffer, buf.cap) is null) {
err := FStr() `Unable to get current directory: $(strerror())`;
throw SystemError(err, errno());
}
View
53 lib/crack/io.crk
@@ -70,14 +70,11 @@ class OwningFDWriter : FDWriter {
# if the amount read is most of the buffer, just take ownership of the
# buffer.
- if (s >= size * 3 / 4) {
- b.size = s;
+ if (s >= size * 3 / 4)
return String(b, true);
- } else {
+ else
# otherwise copy.
- b.size = s;
return String(b);
- }
}
# Since we can't load crack.ann yet, add the interface plumbing manually
@@ -97,9 +94,10 @@ class FDReader : FileHandle, Reader {
## Read up to 'size' bytes from the file descriptor into the buffer.
uint read(WriteBuffer buf) {
- int rc = read(fd, buf.buffer, buf.size);
+ int rc = read(fd, buf.buffer, buf.cap);
if (rc == -1)
throw Exception(String(c_strerror()));
+ buf.size = rc;
return rc;
}
@@ -191,16 +189,16 @@ class StandardFormatter : Formatter {
# have to convert so it will match the later _format method.
int64 v = val;
- start := _format(v, buffer.buffer, buffer.size);
- write(Buffer(buffer.buffer + start, buffer.size - start));
+ start := _format(v, buffer.buffer, buffer.cap);
+ write(Buffer(buffer.buffer + start, buffer.cap - start));
}
void format(uint16 val) {
buffer := ManagedBuffer(5);
# _format(uint32) comes first so we don't have to type convert
- start := _format(val, buffer.buffer, buffer.size);
- write(Buffer(buffer.buffer + start, buffer.size - start));
+ start := _format(val, buffer.buffer, buffer.cap);
+ write(Buffer(buffer.buffer + start, buffer.cap - start));
}
void format(int32 val) {
@@ -208,35 +206,35 @@ class StandardFormatter : Formatter {
# have to convert so it will match the later _format method.
int64 v = val;
- start := _format(v, buffer.buffer, buffer.size);
- write(Buffer(buffer.buffer + start, buffer.size - start));
+ start := _format(v, buffer.buffer, buffer.cap);
+ write(Buffer(buffer.buffer + start, buffer.cap - start));
}
void format(uint32 val) {
buffer := ManagedBuffer(10);
# _format(uint32) comes first so we don't have to type convert
- start := _format(val, buffer.buffer, buffer.size);
- write(Buffer(buffer.buffer + start, buffer.size - start));
+ start := _format(val, buffer.buffer, buffer.cap);
+ write(Buffer(buffer.buffer + start, buffer.cap - start));
}
void format(int64 val) {
buffer := ManagedBuffer(21);
- start := _format(val, buffer.buffer, buffer.size);
- write(Buffer(buffer.buffer + start, buffer.size - start));
+ start := _format(val, buffer.buffer, buffer.cap);
+ write(Buffer(buffer.buffer + start, buffer.cap - start));
}
void format(uint64 val) {
buffer := ManagedBuffer(20);
- start := _format(val, buffer.buffer, buffer.size);
- write(Buffer(buffer.buffer + start, buffer.size - start));
+ start := _format(val, buffer.buffer, buffer.cap);
+ write(Buffer(buffer.buffer + start, buffer.cap - start));
}
# this temporarily uses a runtime call, until we implement a float
# printer in crack
void format(float32 val) {
buffer := ManagedBuffer(80);
- float_str(val, buffer.buffer, buffer.size);
+ float_str(val, buffer.buffer, buffer.cap);
write(buffer.buffer);
}
@@ -244,7 +242,7 @@ class StandardFormatter : Formatter {
# printer in crack
void format(float64 val) {
buffer := ManagedBuffer(80);
- float_str(val, buffer.buffer, buffer.size);
+ float_str(val, buffer.buffer, buffer.cap);
write(buffer.buffer);
}
@@ -291,17 +289,17 @@ class StringWriter : AppendBuffer, Writer {
## Return a string containing everything that has been written so far.
## Deprecated. Use makeString() or string() instead.
String createString() {
- return String(buffer, pos, false);
+ return String(buffer, size, false);
}
## Return a string containing everything that has been written so far.
String makeString() {
- return String(buffer, pos, false);
+ return String(buffer, size, false);
}
## Return a string containing everything that has been written so far.
String string() {
- return String(buffer, pos, false);
+ return String(buffer, size, false);
}
## Return a null terminated string containing everything that has been
@@ -309,21 +307,21 @@ class StringWriter : AppendBuffer, Writer {
## Deprecated. Use makeCString() or cString() instead.
CString createCString() {
append(0);
- return CString(buffer, pos - 1, false);
+ return CString(buffer, size - 1, false);
}
## Return a null terminated string containing everything that has been
## written so far.
CString makeCString() {
append(0);
- return CString(buffer, pos - 1, false);
+ return CString(buffer, size - 1, false);
}
## Return a null terminated string containing everything that has been
## written so far.
CString cString() {
append(0);
- return CString(buffer, pos - 1, false);
+ return CString(buffer, size - 1, false);
}
Object _iface_getWriterObject() { return this; }
@@ -342,9 +340,10 @@ class StringReader : Object, Reader {
return 0;
count :=
- (__pos + buf.size > __src.size) ? __src.size - __pos : buf.size;
+ (__pos + buf.cap > __src.size) ? __src.size - __pos : buf.cap;
buf.move(0, __src.buffer + __pos, count);
__pos += count;
+ buf.size = count;
return count;
}
View
32 lib/crack/io/readers.crk
@@ -16,13 +16,13 @@ LineIter _createLineIter(LineReader reader);
class LineReader {
Reader r;
ManagedBuffer buffer = {1024};
- uint start, end;
+ uint start;
oper init(Reader reader) : r = reader {}
String readLine() {
while (true) {
- for (uint i = start; i < end; ++i) {
+ for (uint i = start; i < buffer.size; ++i) {
if (buffer.buffer[i] == NEWLINE) {
tmp := Buffer(buffer.buffer + start, i + 1 - start);
start = i + 1;
@@ -34,27 +34,30 @@ class LineReader {
# compact the buffer
if (start) {
- buffer.move(0, start, end - start);
- end = end - start;
+ buffer.compact(start);
start = 0;
}
# if there's less than 1K of space available, grow the buffer
- if (end + 1024 > buffer.size)
- buffer.grow(buffer.size + 1024);
+ if (buffer.size + 1024 > buffer.cap)
+ buffer.grow(buffer.cap + 1024);
# read the next block
- amtRead := r.read(WriteBuffer(buffer.buffer + end, 1024));
- end = end + amtRead;
+ amtRead :=
+ r.read(WriteBuffer(buffer.buffer + buffer.size, 0, 1024));
+ buffer.size += amtRead;
# if we are at the end of the file, either return the remaining
# chunk or a null pointer if there is no remaining chunk
if (!amtRead)
- if (start == end) {
+ if (start == buffer.size) {
return null;
} else {
- result := String(Buffer(buffer.buffer + start, end - start));
- start = end;
+ result := String(buffer.buffer + start,
+ buffer.size - start,
+ false
+ );
+ start = buffer.size;
return result;
}
}
@@ -90,12 +93,12 @@ class FullReader : LineReader @implements Reader {
## it as a string.
String readAll() {
# start with the current contents of the buffer
- AppendBuffer result = {end - start};
- result.extend(buffer.buffer + start, end - start);
+ AppendBuffer result = {buffer.size - start};
+ result.extend(buffer.buffer + start, buffer.size - start);
# reset the buffer
start = 0;
- end = 0;
+ buffer.size = 0;
# use a temporary write buffer so we read in chunks
buf := WriteBuffer(buffer.buffer, 1024);
@@ -107,7 +110,6 @@ class FullReader : LineReader @implements Reader {
amtRead = r.read(buf);
}
- result.size = result.pos;
return String(result, true);
}
}
View
4 lib/crack/json.crk
@@ -1027,7 +1027,7 @@ uint JSON_string_en_main = 1;
ParserResult parseString(uint p, uint pe) {
- append_buf.pos = 0;
+ append_buf.size = 0;
String result = null;
uint cs = EVIL;
byte chr;
@@ -1153,7 +1153,7 @@ uint JSON_string_en_main = 1;
if (p > memo)
append_buf.extend(data + uintz(memo), p - memo);
- result = String(append_buf, append_buf.pos, false);
+ result = String(append_buf, append_buf.size, false);
p = ( p + 1) - 1; //EXEC
// ACTION
View
85 lib/crack/lang.crk
@@ -367,35 +367,65 @@ void _formatObject(Formatter f, Object o) {
## A writable buffer. You can assume that it is safe to write to one of these.
class WriteBuffer : Buffer {
- ## constructor inheritence would be nice
- oper init(byteptr buffer, uint size) : Buffer(buffer, size) {}
+ ## buffer capacity
+ uint cap;
- ## Move data from an arbitrary byteptr into the buffer.
+ oper init(byteptr buffer, uint size) : Buffer(buffer, size), cap = size {}
+
+ ## Construct a new buffer from a buffer pointer, an initial size and
+ ## capacity.
+ oper init(byteptr buffer, uint size, uint cap) :
+ Buffer(buffer, size),
+ cap = cap {
+ }
+
+ ## Move data from an arbitrary byteptr into the buffer. The end of the
+ ## new data must not exceed buffer capacity, if it exceeds the current
+ ## size the size will be adjusted to fit it.
void move(uint start, byteptr ptr, uint count) {
- if (start + count > size)
+ if (start + count > cap)
_throwIndexError('Move target out of bounds');
memmove(buffer + start, ptr, count);
+ if (start + count > size)
+ size = start + count;
}
- ## move data from one place in the buffer to another place in the buffer.
+ ## move data from one place in the buffer to another place in the buffer.
+ ## Rules concerning size and capacity in move(uint, byteptr, int) apply.
void move(uint newStart, uint oldStart, uint count) {
- if (oldStart + count > size)
+ if (oldStart + count > cap)
_throwIndexError('Move source out of bounds');
move(newStart, buffer + oldStart, count);
+ if (newStart + count > size)
+ size = newStart + count;
}
## move a buffer to a place in this buffer.
+ ## Rules concerning size and capacity in move(uint, byteptr, int) apply.
void move(uint newStart, Buffer buf) {
- if (newStart + buf.size > size)
+ if (newStart + buf.size > cap)
_throwIndexError('Move target out of bounds');
move(newStart, buf.buffer, buf.size);
+ if (newStart + buf.size > size)
+ size = newStart + buf.size;
+ }
+
+ ## Move the contents of the buffer from pos to size to the beginning of
+ ## the buffer, and modify size accordingly (the new size is size - pos).
+ ## This effectively truncates the beginning of the buffer without doing
+ ## any memory allocation/deallocation.
+ void compact(uint pos) {
+ if (pos > size)
+ _throwIndexError('Compress argument out of bounds');
+ move(0, pos, size - pos);
+ size = size - pos;
}
};
## A managed buffer. Deletes the bytebuf when it goes out of scope.
class ManagedBuffer : WriteBuffer {
- oper init(uint size) : WriteBuffer(malloc(size), size) {}
+ oper init(uint cap) : WriteBuffer(malloc(cap), 0, cap) {}
## Orphan's the byte buffer, returning it for use by the caller. The
## caller is responsible for freeing it.
@@ -406,20 +436,20 @@ class ManagedBuffer : WriteBuffer {
return temp;
}
- ## Grow the buffer to the new size.
- void grow(uint newSize) {
- if (newSize <= size) {
+ ## Grow the buffer to the new capacity.
+ void grow(uint newCap) {
+ if (newCap <= cap) {
puts('attempt to grow to a smaller or same size.');
abort();
}
- newBuffer := malloc(newSize);
+ newBuffer := malloc(newCap);
if (!(buffer is null)) {
memcpy(newBuffer, buffer, size);
free(buffer);
}
buffer = newBuffer;
- size = newSize;
+ cap = newCap;
}
oper del() {
@@ -430,29 +460,29 @@ class ManagedBuffer : WriteBuffer {
## AppendBuffer lets you add stuff onto the end of it.
class AppendBuffer : ManagedBuffer {
- uint pos;
- oper init(uint size) : ManagedBuffer(size) {}
+ oper init(uint cap) : ManagedBuffer(cap) {}
## Append a single byte to the buffer, growing if necessary.
void append(byte c) {
- if (pos >= size)
- grow(size * 2);
- buffer[pos++] = c;
+ if (size >= cap)
+ grow(cap * 2);
+ buffer[size++] = c;
}
- ## Extend the buffer from the pointer and size
+ ## Extend the buffer from the pointer and size. That is, concatenate the
+ ## buffer defined by buf, bufSize onto the end of the current buffer.
#unsafe
@final void extend(byteptr buf, uint bufSize) {
- uint newPos = pos + bufSize;
- if (newPos > size)
- # grow to the larger of newPos and size * 2
- if (newPos > size * 2)
- grow(newPos);
+ uint newSize = size + bufSize;
+ if (newSize > cap)
+ # grow to the larger of newSize and cap * 2
+ if (newSize > cap * 2)
+ grow(newSize);
else
- grow(size * 2);
- move(pos, buf, bufSize);
- pos = newPos;
+ grow(cap * 2);
+ move(size, buf, bufSize);
+ size = newSize;
}
## Extend the buffer with the (entire) other buffer.
@@ -700,7 +730,6 @@ class String : Buffer {
}
buf.append(39);
- buf.size = buf.pos;
return String(buf, true);
}
View
5 lib/crack/net.crk
@@ -190,7 +190,10 @@ class Socket : Pollable @implements Reader, Writer {
}
int recv(WriteBuffer buf, int flags) {
- return recv(fd, buf.buffer, buf.size, flags);
+ rc := recv(fd, buf.buffer, buf.cap, flags);
+ if (rc != -1)
+ buf.size = rc;
+ return rc;
}
class Accepted {
View
5 lib/crack/net/httpsrv.crk
@@ -88,7 +88,6 @@ String joinPath(StringArray path, int start, int end) {
buf.append(b'/');
buf.extend(path[i]);
}
- buf.size = buf.pos;
return String(buf, true);
}
@@ -192,10 +191,10 @@ class HTTPServer {
## Returns a byteptr to the current input buffer read position.
@final byteptr getInputBuf() {
- if (inbuf.size - inPos < 1024)
+ if (inbuf.cap - inPos < 1024)
# since the start size is 1024, we can assume that we will
# grow to accomodate a 1024 byte buffer.
- inbuf.grow(inbuf.size * 2);
+ inbuf.grow(inbuf.cap * 2);
return inbuf.buffer + pos;
}
View
10 lib/crack/process.crk
@@ -72,12 +72,10 @@ class Process {
// extendable buffer used for final string
wr := AppendBuffer(1024);
rd := FDReader(pipe);
- uint size, totalSize;
- while ((size = rd.read(buf)) != 0) {
- wr.extend(buf, size);
- totalSize += size;
- }
- return String(wr, totalSize, true); // takes ownership of appendbuffer
+ uint size;
+ while ((size = rd.read(buf)) != 0)
+ wr.extend(buf);
+ return String(wr, true); // takes ownership of appendbuffer
}
String getStdOut() {
View
10 lib/crack/serial.crk
@@ -73,12 +73,12 @@ class SerialReader {
}
# add null terminator
- mbuf.buffer[mbuf.size - 1] = 0;
+ mbuf.buffer[mbuf.cap - 1] = 0;
# indicate that we've depleted the buffer
pos = buf.size;
- size = mbuf.size - 1;
+ size = mbuf.cap - 1;
return CString(mbuf.orphan(), size, true);
}
@@ -117,8 +117,8 @@ class SerialWriter {
}
# write the temp buffer and reset it.
- __dst.write(Buffer(buf.buffer, buf.pos));
- buf.pos = 0;
+ __dst.write(Buffer(buf.buffer, buf.size));
+ buf.size = 0;
}
void write(String val) {
@@ -130,5 +130,3 @@ class SerialWriter {
__dst.flush();
}
}
-
-
View
4 opt/ini.rl
@@ -92,7 +92,7 @@ class IniParser {
}
action valueStart {
- appendBuf.pos = 0;
+ appendBuf.size = 0;
okp = p+1;
}
@@ -140,7 +140,7 @@ class IniParser {
action valueEnd {
if (p > okp)
appendBuf.extend(data + uintz(okp), p - okp);
- value = String(appendBuf, 0, appendBuf.pos);
+ value = String(appendBuf, 0, appendBuf.size);
_sectionMap[key] = value;
okp = p+1;
View
4 opt/json.rl
@@ -493,7 +493,7 @@ class JsonParser {
action parse_string {
if (p > memo)
append_buf.extend(data + uintz(memo), p - memo);
- result = String(append_buf, append_buf.pos, false);
+ result = String(append_buf, append_buf.size, false);
fexec p + 1;
}
@@ -511,7 +511,7 @@ class JsonParser {
}%%
ParserResult parseString(uint p, uint pe) {
- append_buf.pos = 0;
+ append_buf.size = 0;
String result = null;
uint cs = EVIL;
byte chr;
Please sign in to comment.
Something went wrong with that request. Please try again.