Using the bytestring class

- Introduction
- Manipulating Byte Strings
- Comparing Byte Strings
- Finding Data in Byte Strings

### Introduction

The bytestring class provides methods for manipulating byte strings.

In addition to some unique methods, analogs for the standard C "memory" functions are provided. However, unlike the standard C functions, the bytestring methods are NULL safe. Your application will not crash if a NULL is passed in, and instead, will give intuitive results.

### Manipulating Byte Strings

The bytestring class provides methods for zeroing, initializing, duplicating, and copying byte strings.

> #include <rudiments/bytestring.h>
> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> unsigned char buffer[9];
> // zero the buffer
> bytestring::zero(buffer,sizeof(buffer));
> stdoutput.write("all zeros:\n ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> // set the buffer to all ones
> bytestring::set(buffer,1,sizeof(buffer));
> stdoutput.write("all ones:\n ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> // copy some data into the buffer
> unsigned char data[]={0,1,2,3,4,5,6,7,8};
> bytestring::copy(buffer,data,sizeof(data));
> stdoutput.write("after copying 0-8:\n ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> // copy some overlapping data from one part of the buffer to another
> bytestring::copyWithOverlap(buffer,buffer+3,sizeof(buffer)-3);
> stdoutput.write("after copying with overlap:\n ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> // copy a string into the buffer
> unsigned char str[]="hello";
> bytestring::copyUntil(buffer,str,'\0',sizeof(str));
> stdoutput.write("after copying \"hello\":\n ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> // copy some data into the buffer, swapping bytes as they are copied
> bytestring::copySwapBytes(buffer,data,sizeof(data));
> stdoutput.write("after copying 0-8, swapping bytes:\n ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> }

### Comparing Byte Strings

The bytestring class also provides a compare() method for comparing two byte strings.

> #include <rudiments/bytestring.h>
> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> unsigned char buffer1[8];
> unsigned char buffer2[8];
> // zero the buffers
> bytestring::zero(buffer1,sizeof(buffer1));
> bytestring::zero(buffer2,sizeof(buffer2));
> stdoutput.write("both zeroed:\n");
> stdoutput.printf(" does buffer1=buffer2? %s\n\n",
> (!bytestring::compare(buffer1,buffer2,sizeof(buffer1)))?
> "yes":"no");
> // set buffer2 to all ones
> bytestring::set(buffer2,1,sizeof(buffer2));
> stdoutput.write("buffer2 is all ones:\n");
> stdoutput.printf(" does buffer1=buffer2? %s\n\n",
> (!bytestring::compare(buffer1,buffer2,sizeof(buffer1)))?
> "yes":"no");
> // set the first half of buffer2 to zeros
> // and only compare the first half
> bytestring::set(buffer2,0,sizeof(buffer2)/2);
> stdoutput.write("first half of buffer2 is all zeros:\n");
> stdoutput.printf(" does 1/2 buffer1 = 1/2 buffer2? %s\n\n",
> (!bytestring::compare(buffer1,buffer2,sizeof(buffer1)/2))?
> "yes":"no");
> }

### Finding Data in Byte Strings

The charstring class also provides methods for finding characters or other character strings within character strings.

The findFirst() and findLast() methods return the first/last instance of a character or byte string within a byte string, or NULL if no match is found.

> #include <rudiments/bytestring.h>
> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> // initialize buffer
> unsigned char buffer[]={0,2,0,2,0,2,0};
> stdoutput.write("buffer : ");
> stdoutput.safePrint(buffer,sizeof(buffer));
> stdoutput.write("\n\n");
> // find first and last 0
> unsigned char *firstzero=(unsigned char *)
> bytestring::findFirst(buffer,0,sizeof(buffer));
> unsigned char *lastzero=(unsigned char *)
> bytestring::findLast(buffer,0,sizeof(buffer));
> stdoutput.write("first 0 : ");
> stdoutput.safePrint(firstzero,sizeof(buffer)-(firstzero-buffer));
> stdoutput.write('\n');
> stdoutput.write(" last 0 : ");
> stdoutput.safePrint(lastzero,sizeof(buffer)-(lastzero-buffer));
> stdoutput.write("\n\n");
> // find first and last 0,2,0 patterns
> unsigned char pattern[]={0,2,0};
> unsigned char *firstzerotwozero=(unsigned char *)
> bytestring::findFirst(buffer,sizeof(buffer),
> pattern,sizeof(pattern));
> unsigned char *lastzerotwozero=(unsigned char *)
> bytestring::findLast(buffer,sizeof(buffer),
> pattern,sizeof(pattern));
> stdoutput.write("first 0,2,0 : ");
> stdoutput.safePrint(firstzerotwozero,
> sizeof(buffer)-(firstzerotwozero-buffer));
> stdoutput.write('\n');
> stdoutput.write(" last 0,2,0 : ");
> stdoutput.safePrint(lastzerotwozero,
> sizeof(buffer)-(lastzerotwozero-buffer));
> stdoutput.write("\n\n");
> }