Xyris  0.5
string.cpp File Reference
#include <Library/string.hpp>
+ Include dependency graph for string.cpp:

Go to the source code of this file.

Functions

int strlen (const char *s)
 
char * strcat (char *dest, const char *src)
 
void strcpy (char *dest, const char *src)
 
void strncpy (char *dest, const char *src, size_t len)
 
int strcmp (const char *s1, const char *s2)
 
const char * strstr (const char *haystack, const char *needle)
 
void reverse (char *s)
 
void itoa (int n, char str[])
 
void * memset (void *bufptr, int value, size_t size)
 
int memcmp (const void *ptr1, const void *ptr2, size_t num)
 
void * memmove (void *destptr, const void *srcptr, size_t size)
 
void * memcpy (void *dstptr, const void *srcptr, size_t size)
 

Detailed Description

Standard string and memory utility library.

Author
Keeton Feavel (keeto.nosp@m.nfea.nosp@m.vel@c.nosp@m.edar.nosp@m.ville.nosp@m..edu)
Version
0.3
Date
2020-06-17

Definition in file string.cpp.

Function Documentation

◆ itoa()

void itoa ( int  n,
char  str[] 
)

Converts an integer into its ASCII representation. (This does not have a standard, ANSI implementation.)

Parameters
nNumber to be converted to ASCII
strBuffer to hold result

Definition at line 89 of file string.cpp.

90 {
91  int i, sign;
92  if ((sign = n) < 0)
93  n = -n;
94  i = 0;
95  do {
96  str[i++] = (char)(n % 10 + (int)'0');
97  } while ((n /= 10) > 0);
98 
99  if (sign < 0)
100  str[i++] = '-';
101  str[i] = '\0';
102 
103  reverse(str);
104 }
+ Here is the call graph for this function:

◆ memcmp()

int memcmp ( const void *  ptr1,
const void *  ptr2,
size_t  num 
)

Compares a given number of bytes in memory at pointer A to pointer B.

Parameters
ptr1Source pointer
ptr2Destination pointer
numNumber of bytes
Returns
int Returns 0 if identical. If negative, pointer A is less than than pointer B and vice versa for positive.

Definition at line 114 of file string.cpp.

115 {
116  const unsigned char* a = (const unsigned char*)ptr1;
117  const unsigned char* b = (const unsigned char*)ptr2;
118  for (size_t i = 0; i < num; i++) {
119  if (a[i] < b[i])
120  return (int)-i;
121  else if (a[i] > b[i])
122  return (int)i;
123  }
124  return 0;
125 }

◆ memcpy()

void* memcpy ( void *  dstptr,
const void *  srcptr,
size_t  size 
)
Parameters
dstptr
srcptr
size
Returns
void*

Definition at line 141 of file string.cpp.

142 {
143  unsigned char* dst = (unsigned char*)dstptr;
144  const unsigned char* src = (const unsigned char*)srcptr;
145  for (size_t i = 0; i < size; i++)
146  dst[i] = src[i];
147  return dstptr;
148 }
+ Here is the caller graph for this function:

◆ memmove()

void* memmove ( void *  destination,
const void *  source,
size_t  size 
)

Moves a given number of bytes from the source to the destination.

Parameters
destinationDestination pointer
sourceSource pointer
sizeNumber of bytes
Returns
void* Pointer to the destination

Definition at line 127 of file string.cpp.

128 {
129  unsigned char* dst = (unsigned char*)destptr;
130  const unsigned char* src = (const unsigned char*)srcptr;
131  if (dst < src) {
132  for (size_t i = 0; i < size; i++)
133  dst[i] = src[i];
134  } else {
135  for (size_t i = size; i != 0; i--)
136  dst[i - 1] = src[i - 1];
137  }
138  return destptr;
139 }

◆ memset()

void* memset ( void *  bufptr,
int  value,
size_t  num 
)

Sets the number of bytes in memory at ptr to the value.

Parameters
bufptrPointer to location in memory
valueValue to be written in memory
numNumber of bytes
Returns
void* Pointer to location in memory

Definition at line 106 of file string.cpp.

107 {
108  unsigned char* buf = (unsigned char*)bufptr;
109  for (size_t i = 0; i < size; i++)
110  buf[i] = (unsigned char)value;
111  return bufptr;
112 }
+ Here is the caller graph for this function:

◆ reverse()

void reverse ( char *  s)

Reverses the inputted string.

Parameters
sString to be reversed

Definition at line 78 of file string.cpp.

79 {
80  int c;
81  int j = strlen(s) - 1;
82  for (int i = 0; i < j; i++, j--) {
83  c = s[i];
84  s[i] = s[j];
85  s[j] = (char)c;
86  }
87 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ strcat()

char* strcat ( char *  dest,
const char *  src 
)

Concatanates source onto destination.

Parameters
destDestination
srcSource
Returns
char* Pointer to destination

Definition at line 23 of file string.cpp.

24 {
25  const size_t len_dest = strlen(dest);
26  const size_t len_src = strlen(src);
27  memcpy(dest + len_dest, src, len_src + 1);
28  return dest;
29 }
+ Here is the call graph for this function:

◆ strcmp()

int strcmp ( const char *  s1,
const char *  s2 
)

Compares two strings.

Parameters
s1String one
s2String two
Returns
int Returns a negative value if a value in s1 is less than s2, a positive number in the inverse case, and zero if both string match.

Definition at line 51 of file string.cpp.

52 {
53  while (*s1)
54  {
55  if (*s1 != *s2)
56  break;
57 
58  s1++;
59  s2++;
60  }
61 
62  return (int)*s1 - (int)*s2;
63 }
+ Here is the caller graph for this function:

◆ strcpy()

void strcpy ( char *  destination,
const char *  source 
)

Copys a string from the source to the destination.

Parameters
sourceString to be copied
destinationLocation where string will be copied

Definition at line 31 of file string.cpp.

32 {
33  while (*src) {
34  *dest = *src;
35  dest++;
36  src++;
37  }
38  *dest = '\0';
39 }

◆ strlen()

int strlen ( const char *  s)

Returns the length of a string.

Parameters
sInput string
Returns
int Length of string

Definition at line 14 of file string.cpp.

15 {
16  int i = 0;
17  while (s[i] != '\0') {
18  ++i;
19  }
20  return i;
21 }
+ Here is the caller graph for this function:

◆ strncpy()

void strncpy ( char *  destination,
const char *  source,
size_t  len 
)

Copys a string from the source to the destination.

Parameters
sourceString to be copied
destinationLocation where string will be copied
lenMaximum string length

Definition at line 41 of file string.cpp.

42 {
43  while (*src && len--) {
44  *dest = *src;
45  dest++;
46  src++;
47  }
48  *dest = '\0';
49 }

◆ strstr()

const char* strstr ( const char *  haystack,
const char *  needle 
)

Locates a substring (needle) within a containing string (haystack)

Parameters
haystackString to be searched
needleSubstring to be located
Returns
char* Pointer to the beginning of the substring

Definition at line 65 of file string.cpp.

66 {
67  while (*haystack)
68  {
69  if ((*haystack == *needle) && (strcmp(haystack, needle) == 0)) {
70  return haystack;
71  }
72  haystack++;
73  }
74 
75  return NULL;
76 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:
size
uint16_t size
Definition: regs.hpp:2
strlen
int strlen(const char *s)
Returns the length of a string.
Definition: string.cpp:14
strcmp
int strcmp(const char *s1, const char *s2)
Compares two strings.
Definition: string.cpp:51
reverse
void reverse(char *s)
Reverses the inputted string.
Definition: string.cpp:78
memcpy
void * memcpy(void *dstptr, const void *srcptr, size_t size)
Definition: string.cpp:141