Modules/Strings

From vishap oberon compiler
Jump to navigation Jump to search

Deutsch (de) | English (en) | հայերեն (hy)

Module Strings[edit]

Module Strings provides a set of operations on strings (i.e., on string constants and character arrays, both of which contain the character 0X as a terminator). All positions in strings start at 0.

Definition[edit]

DEFINITION Strings;
  PROCEDURE Length  (s: ARRAY OF CHAR): INTEGER;
  PROCEDURE Insert  (source: ARRAY OF CHAR;
		   pos: INTEGER; 
		   VAR dest: ARRAY OF CHAR);
  PROCEDURE Append  (extra: ARRAY OF CHAR;
		   VAR dest: ARRAY OF CHAR);
  PROCEDURE Delete  (VAR s: ARRAY OF CHAR;
		   pos, n: INTEGER);
  PROCEDURE Replace (source: ARRAY OF CHAR;
		   pos: INTEGER;
		   VAR dest: ARRAY OF CHAR);
  PROCEDURE Extract (source: ARRAY OF CHAR; 
		   pos, n: INTEGER; 
		   VAR dest: ARRAY OF CHAR);
  PROCEDURE Pos     (pattern, s: ARRAY OF CHAR; 
		   pos: INTEGER): INTEGER;
  PROCEDURE Cap     (VAR s: ARRAY OF CHAR);
END Strings.

Operations[edit]

  • Length(s) returns the number of characters in s up to and excluding the first 0X.
  • Insert(src, pos, dst) inserts the string src into the string dst at position pos (0 <=pos<=Length(dst)). If pos = Length(dst),src is appended to dst. If the size of dst is not large enough to hold the result of the operation, the result is truncated so that dst is always terminated with a 0X.
  • Append(s,dst) has the same effect as Insert(s,Length(dst),dst).
  • Delete(s, pos, n) deletes n characters from s starting at position pos (0 <= pos œ Length(s)). If n > Length(s) - pos, the new length of s is pos.
  • Replace(src, pos, dst) has the same effect as Delete(dst, pos, Length(src)) followed by an Insert(src, pos, dst).
  • Extract(src, pos, n, dst) extracts a substring dst with n characters from position pos (0 <= pos œ Length(src)) in src. If n > Length(src) - pos, dst is only the part of src from pos to the end of src, i.e. Length(src) -1. If the size of dst is not large enough to hold the result of the operation, the result is truncated so that dst is always terminated with a 0X.
  • Pos(pat, s, pos) returns the position of the first occurrence of pat in s. Searching starts at position pos. If pat is not found, -1 is returned.
  • Cap(s) replaces each lower case letter within s by its upper case equivalent.

Remarks

  • String assignments and string comparisons are already supported by the language Oberon-2.

Origin[edit]

This module is loosely based on the ISO Modula-2 Strings library but is much simplified. It was edited by Brian Kirk, Nick Walsh, Josef Templ and Hanspeter Mössenbock.


This article was created by using Oakwood Guidelines as a source.