Commit 8ea3d396 authored by Hans Buchmann's avatar Hans Buchmann

copy/cmp/to:unsigned string

parent 33215ff0
......@@ -54,6 +54,7 @@ class String
// value of digit otherwise
};
//-------------------------- implementation/definition
int String::isDecDigit(char ch)
{
/*
......@@ -73,13 +74,11 @@ int String::isHexDigit(char ch)
return -1;
}
//--------------
//implementation
unsigned String::len(char* s)
{
if (s==0) return 0;
unsigned i=0; //index in s
while(s[i]!='\0)
while(s[i]!='\0')
{
i=i+1; //++i i++
}
......@@ -87,12 +86,71 @@ unsigned String::len(char* s)
}
/*
0 1 2 3 4 5 6 7 8 9 10
src->|a|b|c|d|e|f|g|h|i|j|\0
|------------- terminating zero
dst->|x|x|x|x|x|x|x|x|x|x|x|x|x|x|....
x:some bits
after copy
0 1 2 3 4 5 6 7 8 9 10
src->|a|b|c|d|e|f|g|h|i|j|\0
|------------- terminating zero
dst->|a|b|c|d|e|f|g|h|j|j|\0|x|x|x|....
*/
void String::copy(char* dst,char* src)
//sizeof(dst)==8 byte (64 bit) dst is a pointer
//java: dst.length
{
//special cases
if ((src==0) || (dst==0)) return;
//|------------------------- logical or
unsigned i=0; //index in dst/src
while(true)
{
char ch=src[i];
dst[i]=ch; //copy character
if (ch=='\0') return;
i=i+1;
}
}
void String::to(unsigned v,char s[])
/*
s1->AAAAbCCCCCC\0
s2->AAAAcDDD\0
|--->decision
*/
int String::cmp(char* s1,char* s2)
{
unsigned i=0; //index in s1/s2
while(true)
{
auto ch1=s1[i];
auto ch2=s2[i];
if (ch1!=ch2)
{
return ch1-ch2; //
}
//ch1==ch2
if (ch1=='\0') return 0;
i=i+1;
}
}
void String::to(unsigned v,char s[])
{
// 01234567890
// 2^32= 4294967296 exercise how many decimal digits log(2^32)=32*log(2)=32*0.3
//index 0123456789
static const char Digits[]="0123456789"; //array of char
//example v=123
/*
step vi vi/10 vi%10
......@@ -101,6 +159,28 @@ void String::to(unsigned v,char s[])
1 12 1 2
2 1 0 1
*/
char revdig[20];
unsigned i=0; //index in revdig
do
{
auto d=v%10; //0<=d<10: 0 1 2 3 4 5 6 7 8 9
revdig[i]=Digits[d];
i=i+1;
v=v/10;
}while(v!=0);
//revdig 321
//index 0123
// |-----i
unsigned j=0; //index in s
while(i>0)
{
i=i-1;
s[j]=revdig[i];
j=j+1;
}
//s=123
// |---j
s[j]='\0';
}
bool String::to(char s[],unsigned* val)
......@@ -125,19 +205,28 @@ bool String::to(char s[],unsigned* val)
//test
int main(int argc,char** args)
{
//testcase
/*
index: 0 1 2 3 4 5 6
inhalt: A B C D E F \0
*/
//normal
char s[]="ABCDEF";
unsigned v=123;
char s[30]; //defined *not* initialized
String::to(v,s);
/*
char s[]=""; // len=0 empty string
char s[]="AB\0CDEF" // len=2
char* s=0; // len=0 null string
*/
auto len=String::len(s);
std::cout<<"len="<<len<<"\n";
unsigned
+------------+
v | 123|
+------------+
+-----------------------------------------------------------+
s |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
+-----------------------------------------------------------+
x:dont care
String::to(v,s)
decimal
+------------------------------------------------------------+
s |1|2|3|\0|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
+------------------------------------------------------------+
|--terminating zero
*/
std::cout<<"s="<<s<<"\n";
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment