Last active
January 5, 2017 00:25
-
-
Save SeijiEmery/958b4571882cebdf585c to your computer and use it in GitHub Desktop.
Partial cstring impl and for/while/do-while loop explanation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created on 10/5/2015 by Seiji Emery | |
// | |
// Reimplements cstring functions: | |
// http://www.cplusplus.com/reference/cstring/ | |
// | |
// | |
// Pseudocode: | |
// | |
// pseudocode syntax: | |
// <varname> : <type> = <initializer> | |
// | |
// C syntax: | |
// <type> <type prefix(es)> <varname> <type suffix(es)> = <initializer> | |
// | |
// C++ class initialization syntax: | |
// stack memory: | |
// <class type> <varname>; // default constructor | |
// <class type> <varname> (<params...>); | |
// <class type> <varname> {<params...>}; | |
// <class type> <varname> = <class type>(<params...>); | |
// <class type> <varname> = <class type>{<params...>}; | |
// heap memory: | |
// <class type> * <varname> = new <class type> (); // default constructor | |
// <class type> * <varname> = new <class type> (<params...>); | |
// | |
// For loops and while loops: | |
// These are compound statements. | |
// while: | |
// while (<expr>) { <statements> } | |
// => | |
// loop { | |
// if (!<expr>) { | |
// break; | |
// } else { | |
// <statements> | |
// continue; | |
// } | |
// } | |
// do-while: | |
// do { <statements> } while (<expr>); | |
// => | |
// loop { | |
// <statements> | |
// if (!<expr>) | |
// break; | |
// } else { | |
// continue; | |
// } | |
// } | |
// for: | |
// for (<init-expr>; <cond-expr>; <post-expr>) { <statements> } | |
// => | |
// <init-expr>; | |
// loop { | |
// if (<cond-expr>) { | |
// <statements> | |
// } else { | |
// break; | |
// } | |
// <post-expr> | |
// continue; | |
// } | |
// | |
// | |
// function strlen (str: const char *): size_t { | |
// let i: size_t = 0; | |
// loop { | |
// if (str[i] == '\0') { | |
// return i; | |
// } else { | |
// ++i; | |
// continue; | |
// } | |
// } | |
// } | |
// | |
// function strcmp (a: const char *, b: const char *): int { | |
// let i = 0; | |
// loop { | |
// if (a[i] != b[i]) { | |
// return a[i] - b[i]; | |
// } else if (a[i] == '\0') { // => b[i] == 0 since a[i] == b[i] | |
// return 0; | |
// } else { | |
// ++i; | |
// continue; | |
// } | |
// } | |
// } | |
// | |
// function strcpy (dst: char *, src: const char *): char * { | |
// let i = 0; | |
// loop { | |
// dst[i] = src[i]; | |
// if (src[i] == '\0') { | |
// break; | |
// } else { | |
// ++i; | |
// continue; | |
// } | |
// } | |
// return dst; | |
// } | |
// | |
// function strcat (dst: char *, src: const char *): char * { | |
// let i = 0; | |
// // find end of dst | |
// loop { | |
// if (dst[i] == '\0') { | |
// break; | |
// } else { | |
// ++i; | |
// continue; | |
// } | |
// } | |
// | |
// // write src to end of dst | |
// strcpy(dst + i, src); | |
// | |
// return dst; | |
// } | |
// | |
size_t strlen (const char * str) { | |
size_t i = 0; | |
while (str[i] != '\0') { | |
++i; | |
} | |
return i; | |
} | |
int strcmp (const char * a, const char * b) { | |
while (*a != 0 && *b != 0) { | |
if (*a != *b) { | |
return (int)*a - (int)*b; | |
} | |
} | |
return 0; | |
} | |
char * strcpy (char * dst, const char * src) { | |
// Note: dst[i] = src[i] assigns dst[i] to src[i], and returns src[i], | |
// so we can do the assignment and end-of-string check in one expr. | |
for (int i = 0; (dst[i] = src[i]) != '\0'; ++i) {} | |
return dst; | |
} | |
char * strcat (char * dst, const char * src) { | |
int i = 0; | |
// find end of dst | |
while (dst[i] != '\0') | |
++i; | |
// write to end of dst | |
// strcpy(dst + i, src); | |
for (int j = 0; (dst[i+j] = src[j]) != '\0'; ++j) {} | |
return dst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment