📄 string.cpp
字号:
#pragma String
#include <Errors.h>
#include <Character.h>
unsigned int Length(char &s[])
{ unsigned int i;
for (i = 0; i<=HIGH(s); i++) {
if (s[i]==EOS) return i;
};
return HIGH(s)+1
};
unsigned int Occurs(char &left[], char &right[])
{ unsigned int lengR,lengL,i,count;
lengR=Length(right);
lengL=Length(left);
if (lengR==0) return last;
if (lengL < lengR) return last;
else {
for (i=0; i<=(lengL-lengR); i++) {
if (right[0] == left[i]) {
count=1;
for ( ;; ) {
if (count >= lengR) return i;
if (left[count+i] == right[count]) INC(count);
else break;
};
};
}; /* for */
return last;
}; /* if */
};
unsigned int OccursCS(char &left[], char right[])
{
return Occurs(left, right);
};
void Insert (char &s[], char &w[], unsigned int at)
{ unsigned int lengs, lengw, i, j, k;
lengw = Length(w);
lengs = Length(s);
if (at == last) at = lengs;
else if (at > lengs) SoftwareError("String.Insert:at too big");
if (lengs+lengw > HIGH(s)+1) SoftwareError("String.Insert:too big");
else if (lengs+lengw != HIGH(s)+1) s[lengs+lengw] = EOS;
if (lengs > 0) {
j = lengs+lengw-1;
for (i=lengs-1; i>=at; i--) {
s[j] = s[i]; j = j-1;
};
};
k = 0;
for (i=at; i<=at+lengw-1; i++) {s[i] = w[k]; k = k+1; };
};
void InsertCS(char &s[], unsigned int at, char w[])
{
Insert(s, w, at);
};
void Delete(char &s[], unsigned int at, unsigned int N)
{ unsigned int l, i;
if (N > 0) {
l = Length(s);
if (N > l) SoftwareError("String.Delete:N out of range");
else if ((at==last) || (at==l)) ;
else if ((at>l) || (at<N)) SoftwareError("String.Delete:at out of range");
else if (l-at < N) SoftwareError("String.Delete:at out of range");
else {
for (i = 0; i<=l-at-1; i++) {s[at+i-N] = s[at+i] };
};
s[l-N] = EOS;
};
};
void Copy(char &dest[], char &source[])
{ unsigned int N, i;
i=0;
N= HIGH(dest)+1;
for ( ;; ) {
dest[i]=source[i];
if (source[i]==EOS) break;
i=i+1;
N=N-1;
if (N==0) break;
if (i > HIGH(source)) {dest[i] = EOS; break; };
};
};
void CopyCS(char &dest[], char source[])
{
Copy(dest, source);
};
int Compare(char &left[], char &right[])
{ unsigned int i;
if (HIGH(left) <= HIGH(right)) {
for (i = 0; i<=HIGH(left); i++) {
if ((left[i] != right[i]) || (left[i]==EOS)) {
if (left[i] == right[i]) return 0;
if (left[i] < right[i]) return -i-1;
else return i+1;
};
};
if (HIGH(left) == HIGH(right)) return 0;
if (right[HIGH(left)+1] == EOS) return 0;
else return -HIGH(left)-2;
} else {
for (i = 0; i<=HIGH(right); i++) {
if ((left[i] != right[i]) || (left[i]==EOS)) {
if (left[i] == right[i]) return 0;
if (left[i] < right[i]) return -i-1;
else return i+1;
};
};
if (left[HIGH(right)+1] == EOS) return 0;
return HIGH(right)+2;
};
};
int CompareCS(char &left[], char right[])
{
return Compare(left, right);
};
void Fill(char &dest[], char &fill[], unsigned int at, unsigned int N)
{ unsigned int i,j,k;
if (N == 0) {
i = at; j = 0;
k = Length(fill);
while ((i<=HIGH(dest)) && (dest[i]!='\0')) {
dest[i] = fill[j];
i = i+1; j = j+1;
if (j >= k) j = 0;
};
} else {
i = at; j = 0; N = N-1;
k = Length(fill);
while ((i <= HIGH(dest)) && (i-at <= N)) {
dest[i] = fill[j];
i = i+1; j = j+1;
if (j >= k) j = 0;
};
};
};
void FillCS(char &dest[], char fill[], unsigned int at, unsigned int N)
{
Fill(dest, fill, at, N);
};
/* rationale:
- same representation as compiler used: allows compiler generated
assignment with constant strings (eg. also the empty string "" )
it is possible to add further user operators since the structure
of the strings are known. efficient indexing.
- this is a minimal set of operators,
need tool
==== ====
assignment: Copy or the = statement
copying parts: Copy
making strings longer: Insert, InsertCh
making strings shorter: Delete
substring search: Occurs
compare: Compare
access of a character: array indexing
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -