String.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2014 by Wayne C. Gramlich. All rights reserved.
2 
3 #include <assert.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 
9 #include "String.hpp"
10 #include "Memory.hpp"
11 
18 
19 String String__allocate(unsigned int size) {
20  return (String)Memory__allocate(size + 1, "String__allocate");
21 }
22 
30 
31 bool String__equal(String_Const string1, String_Const string2) {
32  return (bool)(strcmp(string1, string2) == 0);
33 }
34 
42 
44  // Set up *variadic_arguments to start after *format*:
45  va_list variadic_arguments;
46  va_start(variadic_arguments, format);
47 
48  // Compute *formatted_size*:
49  char buffer[2];
50  unsigned int formatted_size = vsnprintf(buffer, 0, format, variadic_arguments);
51  // Allocated *formatted*:
52  String formatted =
53  (String)Memory__allocate(formatted_size + 1, "String__format");
54 
55  // Format *formatted*:
56  va_start(variadic_arguments, format);
57  (void)vsnprintf(formatted, formatted_size + 1, format, variadic_arguments);
58 
59  return formatted;
60 }
61 
64 
65 void String__free(String_Const string) {
66  Memory__free((Memory)string);
67 }
68 
74 
75 unsigned int String__size(String_Const string) {
76  unsigned int size = 0;
77  while (*string++ != '\0') {
78  size += 1;
79  }
80  return size;
81 }
82 
83 
89 
90 unsigned int String__to_unsigned(String_Const string) {
91  unsigned int result = 0;
92  while (1) {
93  char character = *string++;
94  if (isdigit(character)) {
95  result = result * 10 + (character - '0');
96  } else {
97  break;
98  }
99  }
100  return result;
101 }
unsigned int String__to_unsigned(String_Const string)
Converts from decimal string into a number and return it.
Definition: String.cpp:90
void Memory__free(Memory memory)
Releases the storage associated with memory.
Definition: Memory.cpp:60
bool String__equal(String_Const string1, String_Const string2)
Returns true if string1 equals string2.
Definition: String.cpp:31
unsigned int String__size(String_Const string)
Returns the size of string.
Definition: String.cpp:75
void String__free(String_Const string)
will free memory assciated with string.
Definition: String.cpp:65
char * String
Copyright (c) 2013-2014 by Wayne C. Gramlich. All rights reserved.
Definition: String.hpp:8
String String__format(String_Const format,...)
Return a formatted version of format.
Definition: String.cpp:43
void * Memory
Memory is a pointer to memory.
Definition: Memory.hpp:27
const char * String_Const
Definition: String.hpp:9
String String__allocate(unsigned int size)
Allocate and return for size characters.
Definition: String.cpp:19
Memory Memory__allocate(unsigned int bytes, String_Const from)
Allocates bytes of memory and returns a pointer to it.
Definition: Memory.cpp:22


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Dec 28 2017 04:06:53