mirror of
https://github.com/vishapoberon/compiler.git
synced 2026-04-06 02:52:24 +00:00
Faster Files.Read, Files.Readline, Strings.Length
This commit is contained in:
parent
2ddbf5d517
commit
20943d67db
2 changed files with 57 additions and 24 deletions
45
src/runtime/Files.Mod
Normal file → Executable file
45
src/runtime/Files.Mod
Normal file → Executable file
|
|
@ -398,6 +398,10 @@ MODULE Files; (* J. Templ 1.12. 89/12.4.95 Oberon files mapped onto Unix files
|
|||
PROCEDURE Read* (VAR r: Rider; VAR x: SYSTEM.BYTE);
|
||||
VAR offset: LONGINT; buf: Buffer;
|
||||
BEGIN
|
||||
IF (r.org = r.buf.org) & (r.offset < r.buf.size) THEN (* Fast case *)
|
||||
x := r.buf.data[r.offset];
|
||||
INC(r.offset)
|
||||
ELSE (* Slow case *)
|
||||
buf := r.buf; offset := r.offset;
|
||||
IF r.org # buf.org THEN
|
||||
Set(r, buf.f, r.org + offset); buf := r.buf; offset := r.offset
|
||||
|
|
@ -411,6 +415,7 @@ MODULE Files; (* J. Templ 1.12. 89/12.4.95 Oberon files mapped onto Unix files
|
|||
ELSE
|
||||
x := 0X; r.eof := TRUE
|
||||
END
|
||||
END
|
||||
END Read;
|
||||
|
||||
PROCEDURE ReadBytes* (VAR r: Rider; VAR x: ARRAY OF SYSTEM.BYTE; n: LONGINT);
|
||||
|
|
@ -635,15 +640,43 @@ Especially Length would become fairly complex.
|
|||
REPEAT Read(R, ch); x[i] := ch; INC(i) UNTIL ch = 0X
|
||||
END ReadString;
|
||||
|
||||
PROCEDURE ReadLine* (VAR R: Rider; VAR x: ARRAY OF CHAR);
|
||||
VAR i: INTEGER;
|
||||
PROCEDURE ReadLine* (VAR r: Rider; VAR x: ARRAY OF CHAR);
|
||||
VAR i: INTEGER; offset, limit: LONGINT; buffer: Buffer; eoln: BOOLEAN; ch: CHAR;
|
||||
BEGIN
|
||||
i := 0; REPEAT Read(R, x[i]); INC(i) UNTIL (x[i-1] = 0X) OR (x[i-1] = 0AX);
|
||||
IF x[i-1] = 0AX THEN DEC(i) END; (* Omit trailing LF *)
|
||||
IF (i > 0) & (x[i-1] = 0DX) THEN DEC(i) END; (* Also omit preceeding trailing CR if present. *)
|
||||
x[i] := 0X; (* Guarantee zero termination. *)
|
||||
i := 0; limit := LEN(x)-1;
|
||||
offset := r.offset; buffer := r.buf;
|
||||
eoln := r.eof OR (limit = 0);
|
||||
WHILE ~eoln DO
|
||||
IF (r.org # buffer.org) OR (offset >= buffer.size) THEN (* Refresh buffer *)
|
||||
IF r.org + offset < buffer.f.len THEN
|
||||
Set(r, buffer.f, r.org+offset);
|
||||
offset := r.offset; buffer := r.buf; eoln := r.eof;
|
||||
ELSE
|
||||
r.eof := TRUE; eoln := TRUE;
|
||||
END
|
||||
END;
|
||||
WHILE (offset < buffer.size) & ~eoln DO
|
||||
ch := SYSTEM.VAL(CHAR, buffer.data[offset]); INC(offset);
|
||||
IF (ch # 0X) & (ch # 0AX) THEN
|
||||
x[i] := ch; INC(i); eoln := i >= limit
|
||||
ELSE
|
||||
eoln := TRUE
|
||||
END
|
||||
END;
|
||||
r.offset := offset;
|
||||
IF i=limit THEN eoln := TRUE END
|
||||
END;
|
||||
IF (i>0) & (x[i-1] = 0DX) THEN DEC(i) END; (* Exclude CR if present at eol *)
|
||||
x[i] := 0X
|
||||
END ReadLine;
|
||||
|
||||
(* old readline code:
|
||||
REPEAT Read(R, x[i]); INC(i) UNTIL (x[i-1] = 0X) OR (x[i-1] = 0AX);
|
||||
IF x[i-1] = 0AX THEN DEC(i) END; ( * Omit trailing LF * )
|
||||
IF (i > 0) & (x[i-1] = 0DX) THEN DEC(i) END; ( * Also omit preceeding trailing CR if present. * )
|
||||
x[i] := 0X; ( * Guarantee zero termination. * )
|
||||
*)
|
||||
|
||||
PROCEDURE ReadNum*(VAR R: Rider; VAR x: ARRAY OF SYSTEM.BYTE);
|
||||
VAR s, b: SYSTEM.INT8; q: SYSTEM.INT64;
|
||||
BEGIN s := 0; q := 0; Read(R, b);
|
||||
|
|
|
|||
4
src/runtime/Strings.Mod
Normal file → Executable file
4
src/runtime/Strings.Mod
Normal file → Executable file
|
|
@ -29,9 +29,9 @@ Strings.Cap(s)
|
|||
-------------------------------------------------------------*)
|
||||
(* added from trianus v4 *)
|
||||
MODULE Strings; (*HM 94-06-22 / *) (* noch 2017-06-21 *)
|
||||
IMPORT Reals;
|
||||
IMPORT Reals, SYSTEM;
|
||||
|
||||
PROCEDURE Length* (s: ARRAY OF CHAR): INTEGER;
|
||||
PROCEDURE Length* (s: ARRAY [1] OF CHAR): INTEGER;
|
||||
VAR i: LONGINT;
|
||||
BEGIN
|
||||
i := 0; WHILE (i < LEN(s)) & (s[i] # 0X) DO INC(i) END;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue