Browse Source

Functions:

* Implemented 'isOctal' to go with 'isDecimal' and 'isHex'
* Implemented 'getHex', 'getDecimal', and 'getOctal' functions which are slightly more efficient than calling 'getBase' with a constant base input.
* Improved how netmask_table is implemented.
Removed some unused, dead, and inefficient code/functions.
General cleanup and improvements, so my eyes don't bleed as much when looking at functions I implemented several years ago.
release/0.19
Jessica James 9 years ago
parent
commit
6e9525f3f5
  1. 3
      .gitignore
  2. 2
      Jupiter/ArrayList.h
  3. 7
      Jupiter/Database.cpp
  4. 846
      Jupiter/Functions.c
  5. 265
      Jupiter/Functions.h
  6. 2
      Jupiter/IRC_Client.cpp
  7. 38
      Jupiter/String_Type_Imp.h
  8. BIN
      Release/Jupiter.lib

3
.gitignore

@ -154,3 +154,6 @@ $RECYCLE.BIN/
# Mac desktop service store files # Mac desktop service store files
.DS_Store .DS_Store
# Visual Studio OpenDB
*.VC.opendb

2
Jupiter/ArrayList.h

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2013-2015 Jessica James. * Copyright (C) 2013-2016 Jessica James.
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above

7
Jupiter/Database.cpp

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2015 Jessica James. * Copyright (C) 2015-2016 Jessica James.
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -144,11 +144,6 @@ bool Jupiter::Database::append(const char *file, Jupiter::DataBuffer &data)
return r; return r;
} }
template<typename T> T *get_ptr(T &in)
{
return &in;
}
bool Jupiter::Database::append(FILE *file, Jupiter::DataBuffer &data) bool Jupiter::Database::append(FILE *file, Jupiter::DataBuffer &data)
{ {
fwrite(std::addressof<const size_t>(data.size()), sizeof(size_t), 1, file); fwrite(std::addressof<const size_t>(data.size()), sizeof(size_t), 1, file);

846
Jupiter/Functions.c

File diff suppressed because it is too large

265
Jupiter/Functions.h

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2013-2015 Jessica James. * Copyright (C) 2013-2016 Jessica James.
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -74,7 +74,7 @@ namespace Jupiter
* @return Pointer to the first instance of a character in str2 being in str1, or nullptr if there is none. * @return Pointer to the first instance of a character in str2 being in str1, or nullptr if there is none.
*/ */
template<typename T = char> inline const T *strpbrk(const T *str1, const T *str2); template<typename T = char> inline const T *strpbrk(const T *str1, const T *str2);
template<typename T = char> inline const T *strpbrk(const T *str1, const T str2); template<typename T = char> inline const T *strpbrk(const T *str, const T element);
/** /**
* @brief Checks if two strings are equal. * @brief Checks if two strings are equal.
@ -94,183 +94,6 @@ extern "C"
#include <stddef.h> #include <stddef.h>
#endif // __cplusplus #endif // __cplusplus
/**
* @brief Case insensitive version of strstr().
*
* @param str1 String to be scanned.
* @param str2 String to be matched.
* @return Pointer to the first occurance of str2 in str1 if it exists, NULL otherwise.
*/
JUPITER_API const char *stristr(const char *str1, const char *str2);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_strlen(const char *str);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_wstrlen(const wchar_t *str);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_strlen8(const uint8_t *str);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_strlen16(const uint16_t *str);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_strlen32(const uint32_t *str);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_strlen64(const uint64_t *str);
/**
* @brief Returns the length of a C-style string.
*
* @param str C-style string to get the length of.
* @return Length of the C-style string.
*/
JUPITER_API size_t Jupiter_vstrlen(const void *str, size_t size);
/**
* @brief Checks if two strings are equal. This function is case-sensitive.
*
* @param str1 String to be compared.
* @param str2 String to be compared.
* @return True if the strings are equal, false otherwise.
*/
JUPITER_API bool streql(const char *str1, const char *str2);
JUPITER_API bool wstreql(const wchar_t *str1, const wchar_t *str2);
/**
* @brief Checks if two strings are equal. This function is not case-sensitive.
*
* @param str1 String to be compared.
* @param str2 String to be compared.
* @return True if the strings are equal, false otherwise.
*/
JUPITER_API bool streqli(const char *str1, const char *str2);
JUPITER_API bool wstreqli(const wchar_t *str1, const wchar_t *str2);
/**
* @brief Checks if a string matches a wildcard format.
* Note: Case sensitive.
*
* @param format Sring containing the wildcard format information.
* @param str2 String to be compared.
* @return True if the string matches the wildcard format, false otherwise.
*/
JUPITER_API bool strmatch(const char *format, const char *str);
JUPITER_API bool wstrmatch(const wchar_t *format, const wchar_t *str);
/**
* @brief Checks if a string matches a wildcard format.
* Note: Case insensitive.
*
* @param format Sring containing the wildcard format information.
* @param str2 String to be compared.
* @return True if the string matches the wildcard format, false otherwise.
*/
JUPITER_API bool strmatchi(const char *format, const char *str);
JUPITER_API bool wstrmatchi(const wchar_t *format, const wchar_t *str);
/**
* @brief Returns a copy of a substring.
*
* @param str String
* @param a String to be compared.
* @param b String to be compared.
* @return String containing the substring, or NULL on malloc failure.
*/
JUPITER_API char *charToChar(const char *str, int a, int b);
JUPITER_API wchar_t *wcharToChar(const wchar_t *str, int a, int b);
/**
* @brief Removes any carriage-returns (\r) and new lines (\n) from a string.
*
* @param str String to be trimed.
*/
JUPITER_API void trim(char *str);
/**
* @brief Returns a copy of a "word" from a string.
*
* @param str String to be parsed.
* @param w Position of the word, starting at 0.
* @return String containing the word on success, or NULL otherwise.
*/
JUPITER_API char *getWord(const char *str, int w);
/**
* @brief Returns the number of occurances of a given character.
*
* @param str String to be scanned.
* @param c Character to be counted.
* @return Total number of occurances of a character in a string.
*/
JUPITER_API unsigned int countSymbol(const char *str, char c);
/**
* @brief Returns the number of space-deliminated words in a string.
*
* @param str String to be scanned.
* @return Total number of space-deliminated words in a string.
*/
JUPITER_API unsigned int wordCount(const char *str);
/**
* @brief Returns the number of newlines in a string.
*
* @param str String to be scanned.
* @return Total number of newlines in a string.
*/
JUPITER_API unsigned int countLines(const char *str);
/**
* @brief Returns a substring in the form of a new String,
*
* @param str String to be scanned.
* @return String representing a single line of the input string on success, NULL otherwise.
*/
JUPITER_API char *getLine(const char *str, unsigned int l);
/**
* @brief Returns the position of the n'th occurance of a character in a string.
*
* @param str String to be scanned.
* @param c Character to find.
* @param n Position of the character to search for.
* @return Position of the n'th occurance of a character if it exists, -1 otherwise.
*/
JUPITER_API int findSymbol(const char *str, char c, int n);
/** /**
* @brief Checks if a character exists in a string. * @brief Checks if a character exists in a string.
* *
@ -279,14 +102,6 @@ JUPITER_API int findSymbol(const char *str, char c, int n);
*/ */
JUPITER_API bool containsSymbol(const char *str, char c); JUPITER_API bool containsSymbol(const char *str, char c);
/**
* @brief Creates a copy of a string.
*
* @param str String to copy.
* @return String containing a copy of the input string on success, NULL otherwise.
*/
JUPITER_API char *makestr(const char *str);
/** /**
* @brief Returns the current time in a string format. * @brief Returns the current time in a string format.
* This will vary depending on locale. * This will vary depending on locale.
@ -332,7 +147,7 @@ JUPITER_API uint64_t getPowerTwo64(uint64_t number);
* @param c Character to check. * @param c Character to check.
* @return True if the character is a hexadecimal digit, false otherwise. * @return True if the character is a hexadecimal digit, false otherwise.
*/ */
JUPITER_API bool Jupiter_isBase(unsigned char c, int base); JUPITER_API bool Jupiter_isBase(unsigned char chr, int base);
/** /**
* @brief Checks if a character is a hexadecimal digit character. (base 16) * @brief Checks if a character is a hexadecimal digit character. (base 16)
@ -340,7 +155,7 @@ JUPITER_API bool Jupiter_isBase(unsigned char c, int base);
* @param c Character to check. * @param c Character to check.
* @return True if the character is a hexadecimal digit, false otherwise. * @return True if the character is a hexadecimal digit, false otherwise.
*/ */
JUPITER_API bool Jupiter_isHex(unsigned char c); JUPITER_API bool Jupiter_isHex(unsigned char chr);
/** /**
* @brief Checks if a character is a decimal digit character. (base 10) * @brief Checks if a character is a decimal digit character. (base 10)
@ -348,7 +163,15 @@ JUPITER_API bool Jupiter_isHex(unsigned char c);
* @param c Character to check. * @param c Character to check.
* @return True if the character is a decimal digit, false otherwise. * @return True if the character is a decimal digit, false otherwise.
*/ */
JUPITER_API bool Jupiter_isDecimal(unsigned char c); JUPITER_API bool Jupiter_isDecimal(unsigned char chr);
/**
* @brief Checks if a character is an octal digit character. (base 10)
*
* @param c Character to check.
* @return True if the character is a octal digit, false otherwise.
*/
JUPITER_API bool Jupiter_isOctal(unsigned char chr);
/** /**
* @brief Fetches a character's represented integral value. * @brief Fetches a character's represented integral value.
@ -358,7 +181,31 @@ JUPITER_API bool Jupiter_isDecimal(unsigned char c);
* @param base Base of the representation. * @param base Base of the representation.
* @return A character's represented integral value on success, -1 otherwise. * @return A character's represented integral value on success, -1 otherwise.
*/ */
JUPITER_API int Jupiter_getBase(unsigned char c, int base); JUPITER_API int Jupiter_getBase(unsigned char chr, int base);
/**
* @brief Fetches a hexadecimal character's represented integral value. (base 16)
*
* @param c Character to fetch value of.
* @return A character's represented integral value on success, -1 otherwise.
*/
JUPITER_API int Jupiter_getHex(unsigned char chr);
/**
* @brief Fetches a decimal character's represented integral value. (base 10)
*
* @param c Character to fetch value of.
* @return A character's represented integral value on success, -1 otherwise.
*/
JUPITER_API int Jupiter_getDecimal(unsigned char chr);
/**
* @brief Fetches a octal character's represented integral value. (base 8)
*
* @param c Character to fetch value of.
* @return A character's represented integral value on success, -1 otherwise.
*/
JUPITER_API int Jupiter_getOctal(unsigned char chr);
/** /**
* @brief Interprets a string into an integer. * @brief Interprets a string into an integer.
@ -474,20 +321,20 @@ JUPITER_API uint32_t Jupiter_prefix_length_to_netmask(uint8_t prefix_length);
/** strlen implementation */ /** strlen implementation */
template<typename T> inline size_t Jupiter::strlen(const T *str) template<typename T> inline size_t Jupiter::strlen(const T *str)
{ {
register const T *s = str; const T *s = str;
while (*s != 0) s++; while (*s != 0)
++s;
return s - str; return s - str;
} }
/** strcpy implementation */ /** strcpy implementation */
template<typename T> inline T *Jupiter::strcpy(T *dest, const T *source) template<typename T> inline T *Jupiter::strcpy(T *dest, const T *source)
{ {
register T *d = dest; T *d = dest;
while (*source != 0) while (*source != 0)
{ {
*d = *source; *d = *source;
d++; ++d, ++source;
source++;
} }
*d = *source; *d = *source;
return dest; return dest;
@ -499,7 +346,7 @@ template<typename T> inline T *Jupiter::strcpy(T *dest, const T *source, size_t
dest[length] = 0; dest[length] = 0;
while (length > 0) while (length > 0)
{ {
length--; --length;
dest[length] = source[length]; dest[length] = source[length];
} }
return dest; return dest;
@ -513,32 +360,32 @@ template<typename T> inline const T *Jupiter::strpbrk(const T *str1, const T *st
s = str2; s = str2;
while (*s != 0) while (*s != 0)
{ {
if (*str1 == *s) return str1; if (*str1 == *s)
s++; return str1;
++s;
} }
str1++; ++str1;
} }
return nullptr; return nullptr;
} }
template<typename T> inline const T *Jupiter::strpbrk(const T *str1, const T e) template<typename T> inline const T *Jupiter::strpbrk(const T *str, const T element)
{ {
while (*str1 != 0) while (*str != 0)
{ {
if (*str1 == e) return str1; if (*str == element)
str1++; return str;
++str;
} }
return nullptr; return nullptr;
} }
template<typename T> inline bool Jupiter::streql(const T *str1, const T *str2) template<typename T> inline bool Jupiter::streql(const T *str1, const T *str2)
{ {
if (str1 == str2) return true; if (str1 == str2)
return true;
while (*str1 != 0 && *str1 == *str2) while (*str1 != 0 && *str1 == *str2)
{ ++str1, ++str2;
str1++;
str2++;
}
return (*str1 == *str2); return (*str1 == *str2);
} }

2
Jupiter/IRC_Client.cpp

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2013-2015 Jessica James. * Copyright (C) 2013-2016 Jessica James.
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above

38
Jupiter/String_Type_Imp.h

@ -1,5 +1,5 @@
/** /**
* Copyright (C) 2014-2015 Jessica James. * Copyright (C) 2014-2016 Jessica James.
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -187,24 +187,24 @@ template<> inline void Jupiter::String_Type<char>::processEscapeSequences()
case '1': case '1':
case '2': case '2':
case '3': case '3':
if (index + 1 != Jupiter::String_Type<char>::length && Jupiter_isBase(this->get(index + 1), 8)) if (index + 1 != Jupiter::String_Type<char>::length && Jupiter_isOctal(this->get(index + 1)))
{ {
if (index + 2 != Jupiter::String_Type<char>::length && Jupiter_isBase(this->get(index + 2), 8)) if (index + 2 != Jupiter::String_Type<char>::length && Jupiter_isOctal(this->get(index + 2)))
this->replace(index - 1, 4, static_cast<uint8_t>(Jupiter_getBase(this->get(index), 8)) << 6 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 1), 8)) << 3 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 2), 8))); this->replace(index - 1, 4, static_cast<uint8_t>(Jupiter_getOctal(this->get(index))) << 6 | static_cast<uint8_t>(Jupiter_getOctal(this->get(index + 1))) << 3 | static_cast<uint8_t>(Jupiter_getOctal(this->get(index + 2))));
else else
this->replace(index - 1, 3, static_cast<uint8_t>(Jupiter_getBase(this->get(index), 8)) << 3 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 1), 8))); this->replace(index - 1, 3, static_cast<uint8_t>(Jupiter_getOctal(this->get(index))) << 3 | static_cast<uint8_t>(Jupiter_getOctal(this->get(index + 1))));
} }
else else
this->replace(index - 1, 2, static_cast<uint8_t>(Jupiter_getBase(this->get(index), 8))); this->replace(index - 1, 2, static_cast<uint8_t>(Jupiter_getOctal(this->get(index))));
break; break;
case '4': case '4':
case '5': case '5':
case '6': case '6':
case '7': case '7':
if (index + 1 != Jupiter::String_Type<char>::length && Jupiter_isBase(this->get(index + 1), 8)) if (index + 1 != Jupiter::String_Type<char>::length && Jupiter_isOctal(this->get(index + 1)))
this->replace(index - 1, 3, static_cast<uint8_t>(Jupiter_getBase(this->get(index), 8)) << 3 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 1), 8))); this->replace(index - 1, 3, static_cast<uint8_t>(Jupiter_getOctal(this->get(index))) << 3 | static_cast<uint8_t>(Jupiter_getOctal(this->get(index + 1))));
else else
this->replace(index - 1, 2, static_cast<uint8_t>(Jupiter_getBase(this->get(index), 8))); this->replace(index - 1, 2, static_cast<uint8_t>(Jupiter_getOctal(this->get(index))));
break; break;
case 'a': case 'a':
this->replace(index - 1, 2, '\a'); this->replace(index - 1, 2, '\a');
@ -241,20 +241,20 @@ template<> inline void Jupiter::String_Type<char>::processEscapeSequences()
break; break;
case 'x': case 'x':
if (Jupiter::String_Type<char>::length >= index + 2 if (Jupiter::String_Type<char>::length >= index + 2
&& Jupiter_isBase(this->get(index + 1), 16) && Jupiter_isBase(this->get(index + 2), 16)) && Jupiter_isHex(this->get(index + 1)) && Jupiter_isHex(this->get(index + 2)))
this->replace(index - 1, 4, static_cast<uint8_t>(Jupiter_getBase(this->get(index + 1), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 2), 16))); this->replace(index - 1, 4, static_cast<uint8_t>(Jupiter_getHex(this->get(index + 1))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 2))));
break; break;
case 'U': case 'U':
if (Jupiter::String_Type<char>::length >= index + 8 if (Jupiter::String_Type<char>::length >= index + 8
&& Jupiter_isBase(this->get(index + 1), 16) && Jupiter_isBase(this->get(index + 2), 16) && Jupiter_isBase(this->get(index + 3), 16) && Jupiter_isBase(this->get(index + 4), 16) && Jupiter_isBase(this->get(index + 5), 16) && Jupiter_isBase(this->get(index + 6), 16) && Jupiter_isBase(this->get(index + 7), 16) && Jupiter_isBase(this->get(index + 8), 16)) && Jupiter_isHex(this->get(index + 1)) && Jupiter_isHex(this->get(index + 2)) && Jupiter_isHex(this->get(index + 3)) && Jupiter_isHex(this->get(index + 4)) && Jupiter_isHex(this->get(index + 5)) && Jupiter_isHex(this->get(index + 6)) && Jupiter_isHex(this->get(index + 7)) && Jupiter_isHex(this->get(index + 8)))
{ {
uint32_t codepoint = static_cast<uint8_t>(Jupiter_getBase(this->get(index + 1), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 2), 16)); uint32_t codepoint = static_cast<uint8_t>(Jupiter_getHex(this->get(index + 1))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 2)));
codepoint <<= 8; codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getBase(this->get(index + 3), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 4), 16)); codepoint |= static_cast<uint8_t>(Jupiter_getHex(this->get(index + 3))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 4)));
codepoint <<= 8; codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getBase(this->get(index + 5), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 6), 16)); codepoint |= static_cast<uint8_t>(Jupiter_getHex(this->get(index + 5))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 6)));
codepoint <<= 8; codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getBase(this->get(index + 7), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 8), 16)); codepoint |= static_cast<uint8_t>(Jupiter_getHex(this->get(index + 7))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 8)));
if (codepoint <= 0x007F) if (codepoint <= 0x007F)
this->replace(index - 1, 10, static_cast<uint8_t>(codepoint)); this->replace(index - 1, 10, static_cast<uint8_t>(codepoint));
else if (codepoint <= 0x07FF) else if (codepoint <= 0x07FF)
@ -288,11 +288,11 @@ template<> inline void Jupiter::String_Type<char>::processEscapeSequences()
break; break;
case 'u': case 'u':
if (Jupiter::String_Type<char>::length >= index + 4 if (Jupiter::String_Type<char>::length >= index + 4
&& Jupiter_isBase(this->get(index + 1), 16) && Jupiter_isBase(this->get(index + 2), 16) && Jupiter_isBase(this->get(index + 3), 16) && Jupiter_isBase(this->get(index + 4), 16)) && Jupiter_isHex(this->get(index + 1)) && Jupiter_isHex(this->get(index + 2)) && Jupiter_isHex(this->get(index + 3)) && Jupiter_isHex(this->get(index + 4)))
{ {
uint16_t codepoint = static_cast<uint8_t>(Jupiter_getBase(this->get(index + 1), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 2), 16)); uint16_t codepoint = static_cast<uint8_t>(Jupiter_getHex(this->get(index + 1))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 2)));
codepoint <<= 8; codepoint <<= 8;
codepoint |= static_cast<uint8_t>(Jupiter_getBase(this->get(index + 3), 16)) << 4 | static_cast<uint8_t>(Jupiter_getBase(this->get(index + 4), 16)); codepoint |= static_cast<uint8_t>(Jupiter_getHex(this->get(index + 3))) << 4 | static_cast<uint8_t>(Jupiter_getHex(this->get(index + 4)));
if (codepoint <= 0x007F) if (codepoint <= 0x007F)
this->replace(index - 1, 6, static_cast<uint8_t>(codepoint)); this->replace(index - 1, 6, static_cast<uint8_t>(codepoint));
else if (codepoint <= 0x07FF) else if (codepoint <= 0x07FF)

BIN
Release/Jupiter.lib

Binary file not shown.
Loading…
Cancel
Save