scire
Sadh's C++ Impromptu Routines Ensemble
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cstr_util.hpp
Go to the documentation of this file.
1 // scire/string/cstr_util
2 
3 // Copyright (c) 2014, Khan 'Sadh' Mostafa (http://nafSadh.com/Khan)
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying text at http://www.boost.org/LICENSE_1_0.txt)
6 
7 /**
8 scire/string/cstr_util.hpp
9 
10 scire <thing> implementation:
11 - CharStringUtil : very brief description
12 
13 other required scire files:
14  knuth_morris_pratt.hpp
15 
16 author:
17 ~nafSadh
18 */
19 #ifndef SCIRE_string_cstr_util_HPP
20 #define SCIRE_string_cstr_util_HPP
21 
22 #include <cstring>
24 
25 namespace scire
26 {
27 
29 #define SCIRE_CharStringUtil_FUNCS
30 
31  /**
32  */
34  {
35  public:
36  /**
37  * check if all characater in a string is unique
38  * @return false if at least one char appear more than once in the string
39  */
40  static bool IsAllCharUnique(const char str[]/**< string to check */)
41  {
42  size_t len = strlen(str);
43  if (len > 128) return false;
44 
45  bool chrmap[128] = {false};
46  size_t i = 0;
47  while (i < len) {
48  if (chrmap[str[i]] == true)
49  return false;
50  chrmap[str[i]] = true;
51  i++;
52  }
53 
54  return true;
55  }
56 
57  /** reverse the passed string */
58  static void Reverse(char str[] /**<string to reverse */)
59  {
60  size_t len = strlen(str);
61  size_t mid = len / 2;
62  size_t end = len - 1;
63 
64  for (size_t i = 0; i < mid; i++) {
65  char temp = str[i];
66  str[i] = str[end - i];
67  str[end - i] = temp;
68  }
69  }
70 
71  /** check if a string passed is a permutation of the other */
72  template <size_t N>
73  static bool IsPermutation(const char a[], const char b[])
74  {
75  size_t len = strlen(a);
76 
77  if (len != strlen(b)) {
78  return false;
79  }
80 
81  int map[N] = {0};
82 
83  size_t i = 0;
84 
85  while (i < len) {
86  map[a[i]]++;
87  map[b[i]]--;
88  i++;
89  }
90 
91  for (i = 0; i < N; i++) {
92  if (map[i] != 0) return false;
93  }
94 
95  return true;
96  }
97 
98  /** replace all occurrences of a character with some string */
99  static size_t Replace(const char subject[],/**<string to operate on */
100  char result[],/**<result string*/
101  char search,/**<character to replace*/
102  char replace[]/**<string to replace with*/)
103  {
104  size_t len = 0;
105 
106  size_t rep = strlen(replace);
107 
108  size_t i = 0;
109  while (subject[i] != '\0') {
110  if (subject[i] == search) {
111  for (size_t j = 0; j < rep; j++) {
112  result[len++] = replace[j];
113  }
114  } else {
115  result[len++] = subject[i];
116  }
117  i++;
118  }
119  result[len] = '\0';
120  return len;
121  }
122 
123  template <typename Type>
125  {
126  size_t base = 10;
127  size_t len;
128 
129  //estimate place count
130  len = 0;
131  Type copy = num;
132  while (copy > 0) {
133  len++;
134  copy /= base;
135  }
136 
137  for (int i = len - 1; i >= 0; i--) {
138  str[i] = (num % base) + '0';
139  num /= base;
140  }
141  str[len] = '\0';
142 
143  return len;
144  }
145 
146  /** encode a string such that every repeated character is encoded as the
147  character followed by count. e.g. aabcccaaaa -> a2b1c3a4
148  @return lenght of encoded string */
149  static size_t CharCountEncoding(const char subject[],/**<string to encode*/
150  char encstr[]/**<encoded string*/)
151  {
152  size_t len = 0;
153 
154  size_t i = 0;
155  char curc = subject[i];
156  size_t count = 1;
157  while (subject[i] != '\0') {
158  if (subject[i+1] == curc) {
159  count++;
160  } else { //update encoded string
161  encstr[len++] = curc;
162  //encstr[len++] = count + '0';
163  char nstr[32];
164  size_t nlen = NumToDecimalString(count, nstr);
165  for (size_t j = 0; j < nlen; j++) {
166  encstr[len++] = nstr[j];
167  }
168  //reset
169  curc = subject[i + 1];
170  count = 1;
171  }
172  i++;
173  }
174  encstr[len] = '\0';
175 
176  return len;
177  }
178 
179  /** compress a string such that every repeated character is encoded as the
180  character followed by count. e.g. aabcccaaaa -> a2b1c3a4. If encoded string
181  is not smaller then return original string.
182  @return lenght of encoded string */
184  const char subject[],/**<string to encode*/
185  char compstr[]/**<encoded string*/)
186  {
187  size_t enclen = CharCountEncoding(subject, compstr);
188  if (enclen < strlen(subject))
189  return enclen;
190  else {
191  size_t i = 0;
192  while (subject[i] != '\0') {
193  compstr[i] = subject[i];
194  i++;
195  }
196  compstr[i] = '\0';
197  return i;
198  }
199  }
200 
201  /** Find the position of the first occurrence of a substring in a text
202  @return position (index) of first occurrence of substring in text if found,
203  if not found then the lenght of text */
204  static size_t StrPos(
205  const char* haystack,/**< text to search in */
206  const char* needle, /**< substring to look for */
207  size_t offset = 0/** offset to start search from */
208  )
209  {
210  return StringMatchKMP<>::Position(
211  haystack + offset, strlen(haystack) - offset,
212  needle, strlen(needle));
213  }
214 
215  /** check if a string is a substring of other */
216  static bool isSubstring(
217  const char* string, /**< text to find substring in (haystack) */
218  const char* substring/**< substring to look for (needle) */
219  )
220  {
221  size_t pos = StrPos(string, substring);
222  if (strlen(string) == pos)
223  return false;
224  else return true;
225  }
226 
227  /** check if a string is a roation of another */
228  static bool isRotation(
229  const char* str,
230  const char* rstr
231  )
232  {
233  size_t len = strlen(str);
234  if (strlen(rstr) != len) return false;
235 
236  size_t dublen = len * 2 + 1;
237  char* strstr = new char[dublen];
238  strstr[0] = '\0';
239  strcat_s(strstr, dublen, str);
240  strcat_s(strstr, dublen, str);
241 
242  return isSubstring(strstr, rstr);
243  }
244  };
245 #endif//SCIRE_CharStringUtil_FUNCS
246 }
247 #endif//SCIRE_string_cstr_util_HPP
static bool isRotation(const char *str, const char *rstr)
check if a string is a roation of another
Definition: cstr_util.hpp:228
static size_t CharCountCompress(const char subject[], char compstr[])
compress a string such that every repeated character is encoded as the character followed by count...
Definition: cstr_util.hpp:183
static size_t StrPos(const char *haystack, const char *needle, size_t offset=0)
Find the position of the first occurrence of a substring in a text.
Definition: cstr_util.hpp:204
static size_t Replace(const char subject[], char result[], char search, char replace[])
replace all occurrences of a character with some string
Definition: cstr_util.hpp:99
static size_t CharCountEncoding(const char subject[], char encstr[])
encode a string such that every repeated character is encoded as the character followed by count...
Definition: cstr_util.hpp:149
static bool isSubstring(const char *string, const char *substring)
check if a string is a substring of other
Definition: cstr_util.hpp:216
static size_t NumToDecimalString(Type num, char str[])
Definition: cstr_util.hpp:124
static bool IsPermutation(const char a[], const char b[])
check if a string passed is a permutation of the other
Definition: cstr_util.hpp:73
static void Reverse(char str[])
reverse the passed string
Definition: cstr_util.hpp:58
scire/graph/gale_shapley.hpp
static bool IsAllCharUnique(const char str[])
check if all characater in a string is unique
Definition: cstr_util.hpp:40