copypaste

This commit is contained in:
2025-06-02 09:10:50 -04:00
parent 87ccfdf279
commit eb9ae4316f
15 changed files with 3777 additions and 0 deletions

View File

@ -4,11 +4,163 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <vector>
#include <string>
#include <limits>
#include <locale>
#include <amsmathutil25/amsmathutil25.hpp>
namespace ams
{
//wraps the functions strcpy_s and strncpy in a portable manner
//between linux and microsoft standard C libraries.
int amsstrcpy_s(char *dest, int size, const char *src);
//wraps the functions sprintf_s and snprintf in a portable manner
//between linux and microsoft standard C libraries.
int amssprintf_s(char *s, int n, const char *format, ...);
//Using the C library's sscanf function is more robust
//than atod or atof. It returns valid numbers for infs and nans
//Returns nan for any uninterpretable string
double amsstrtonum(const char *str);
typedef char ams_chartype;
static const ams_chartype ams_char_cr = (ams_chartype) '\r'; //carriage return
static const ams_chartype ams_char_lf = (ams_chartype) '\n'; //newline
static const ams_chartype ams_char_tb = (ams_chartype) '\t'; //tab
static const ams_chartype ams_char_nt = (ams_chartype) '\0'; //null terminator
class amsstring
{
public:
ams_chartype blank; // null terminator returned for accessing index out of bounds
ams_chartype *cstring;
int length;
//length will be set to the length of the cstring not including the null terminating char
//Basic functions
amsstring();
~amsstring();
amsstring(amsstring &other);
amsstring& operator=(amsstring &other);
amsstring(const amsstring &other);
const amsstring& operator=(const amsstring &other);
amsstring(ams_chartype *other);
amsstring(const ams_chartype *other);
amsstring& operator=(ams_chartype *other);
const amsstring& operator=(const ams_chartype *other); //assign string constant to amsstring
//const amsstring& operator=(const ams_chartype *other) const; //assign string constant to amsstring
//const is a disease!
//
//amsstring(int length);
//amsstring(int length, const ams_chartype initchar);
int resize(const int newlen);
int size() const;
ams_chartype& operator[](const int ind);
const ams_chartype& operator[](const int ind) const;
ams_chartype& at(const int ind);
const ams_chartype& at(const int ind) const;
void clear();
void setall(const ams_chartype val, const int newlen);
void shrinktofit();
//string comparisons
bool operator==(const amsstring &other) const;
bool operator==(const char *other) const;
bool operator!=(const amsstring &other) const;
bool operator!=(const char *other) const;
//string ordering comparison
//alphebetizes strings by ASCII character
//longer strings compare larger than shorter ones
bool operator<(const amsstring &other) const;
bool operator>(const amsstring &other) const;
bool operator<(const ams_chartype *other) const;
bool operator>(const ams_chartype *other) const;
//Insert, Remove, and Substring
void insert(const int ind, const amsstring other);
void insert(const int ind, const ams_chartype *other);
void remove(const int ind);
void remove(const int ind1, const int ind2);
void substring(const int ind1, const int ind2, amsstring *sout) const;
//Append
void append(const amsstring &other);
void append(const ams_chartype *other);
void append(const ams_chartype other);
amsstring operator+(const amsstring &other);
const amsstring operator+(const amsstring &other) const;
amsstring operator+(const ams_chartype *other);
const amsstring operator+(const ams_chartype *other) const;
amsstring operator+(const ams_chartype other);
const amsstring operator+(const ams_chartype other) const;
//Find
int find(const amsstring findstr, const int indstart=0, const bool casesens=1) const;
int find(const ams_chartype *findstr, const int indstart=0, const bool casesens=1) const;
int find(const ams_chartype c, const int indstart=0, const bool casesens=1) const;
//formatted input
int sprintf(int bufflen, const ams_chartype *formatstring, ...);
void tolower();
void toupper();
bool isvalidnumber();
double strtonum();
};
//needs work
void splitlines(amsstring *s, std::vector<amsstring> *lns);
void splitlines(amsstring *s, ams::amsarray<amsstring> *lns);
void split(amsstring *s, const ams_chartype delimitchar, std::vector<amsstring> *lns);
void split(amsstring *s, const ams_chartype delimitchar, ams::amsarray<amsstring> *lns);
void split(amsstring *s, const ams_chartype *delimitstr, std::vector<amsstring> *lns);
void split(amsstring *s, const ams_chartype *delimitstr, ams::amsarray<amsstring> *lns);
void split(amsstring *s, amsstring *delimitstr, std::vector<amsstring> *lns);
void split(amsstring *s, amsstring *delimitstr, ams::amsarray<amsstring> *lns);
//splits a string, not counting whitespaces between non-whitespace characters
void splitwhitespace(amsstring *s, std::vector<amsstring> *lns);
void splitwhitespace(amsstring *s, ams::amsarray<amsstring> *lns);
//removes all whitespace characters '\t','\r','\n' included
//to the left and right of the string (but not in the middle)
void stripwhitespace(amsstring *s);
//completely removes all whitespace entirely
void stripallwhitespace(amsstring *s);
void freadline(FILE *fp, amsstring *s);
void freadlines(FILE *fp, std::vector<amsstring> *lines);
void fwritelines(FILE *fp, amsstring *s);
void fwritelines(FILE *fp, std::vector<amsstring> *lines);
void freadtxtfile(FILE *fp, amsstring *s);
}; //end namespace ams
#include <amsstring4/amsstring4_unicode.hpp>
#include <amsstring4/amsstring4_bintextencoding.hpp>
#include <amsstring4/amsstring4_tests.hpp>
#endif

View File

@ -0,0 +1,28 @@
#ifndef __AMSSTRING4_BINTEXTENCODING_HPP__
#define __AMSSTRING4_BINTEXTENCODING_HPP__
namespace ams
{
//Only processes strings of length divisible by 4, with
//expected 0,1,2 padding chars at the end of the string,
//and no non-coding characters.
void base64encode(ams::amsarray<uint8_t> *bytes, amsstring *str);
int base64decode(amsstring *str, ams::amsarray<uint8_t> *bytes, bool bstrict);
//decodes, ignoring (as in MIME spec) all characters that are not
//valid b64 alphabet chars, and all padding until the end of the string
int base64decode_liberal(amsstring *str, ams::amsarray<uint8_t> *bytes);
int base64decode_strict(amsstring *str, ams::amsarray<uint8_t> *bytes);
void test_base64encode();
void test_base64encode_fuzztest();
};
#endif

View File

@ -0,0 +1,27 @@
#ifndef __AMSSTRING4_TESTS_HPP__
#define __AMSSTRING4_TESTS_HPP__
namespace ams
{
void amsstring3_basic_string_test1();
void amsstring3_sscanf_test1();
void amsstring3_basic_string_test2();
void amsstring3_memoryleakcheck1();
void amsstring3_memoryleakcheck2();
void amsstring3_stringtests2();
void amsstring3_test_find();
void amsstring3_test_splitlines();
void amsstring3_test_split();
void amsstring3_test_strip();
void amsstring3_test_freadwrite();
void amsstring3_test_concatenation_operators();
};
#endif

View File

@ -0,0 +1,22 @@
#ifndef __AMSSTRING4_UNICODE_HPP__
#define __AMSSTRING4_UNICODE_HPP__
namespace ams
{
int string_to_uccodepoints(const amsstring &str, amsarray<uint32_t> &codepoints);
int string_to_uccodepoints(const amsstring *str, amsarray<uint32_t> *codepoints);
void uccodepoints_to_string(const amsarray<uint32_t> &codepoints, amsstring &str);
void uccodepoints_to_string(const amsarray<uint32_t> *codepoints, amsstring *str);
void test_unicode_ascii_int_conv();
void test_unicode_conv1();
void test_unicode_conv2();
};
#endif