ppsspp/base/stringutil.h

57 lines
1.3 KiB
C
Raw Normal View History

2012-03-24 23:39:19 +01:00
#pragma once
#include <cstdlib>
#include <cstdio>
#include <cstring>
2012-03-24 23:39:19 +01:00
#include <string>
#include "base/basictypes.h"
2012-04-27 00:48:30 +02:00
#ifdef _MSC_VER
#pragma warning (disable:4996)
#endif
2012-03-31 11:16:13 +02:00
// Dumb wrapper around itoa, providing a buffer. Declare this on the stack.
2012-03-24 23:39:19 +01:00
class ITOA {
public:
2012-05-08 22:04:24 +02:00
char buffer[16];
const char *p(int i) {
sprintf(buffer, "%i", i);
return &buffer[0];
}
2012-03-24 23:39:19 +01:00
};
2012-03-31 11:16:13 +02:00
// Other simple string utilities.
2012-03-24 23:39:19 +01:00
inline bool endsWith(const std::string &str, const std::string &what) {
2012-05-08 22:04:24 +02:00
return str.substr(str.size() - what.size()) == what;
2012-03-31 11:16:13 +02:00
}
2012-03-27 22:54:58 +02:00
void DataToHexString(const uint8 *data, size_t size, std::string *output);
inline void StringToHexString(const std::string &data, std::string *output) {
DataToHexString((uint8_t *)(&data[0]), data.size(), output);
}
2012-03-27 22:54:58 +02:00
// highly unsafe and not recommended.
unsigned int parseHex(const char* _szValue);
2012-08-13 20:11:24 +02:00
// Suitable for inserting into maps, unlike char*, and cheaper than std::string.
// Strings must be constant and preferably be stored in the read-only part
// of the binary.
class ConstString {
public:
ConstString(const char *ptr) {
ptr_ = ptr;
}
bool operator <(const ConstString &other) const {
return strcmp(ptr_, other.ptr_) < 0;
}
bool operator ==(const ConstString &other) const {
return ptr_ == other.ptr_ || !strcmp(ptr_, other.ptr_);
}
private:
const char *ptr_;
};