-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
#include "SUAPI-CppWrapper/String.hpp" | ||
|
||
#include <cassert> | ||
#include <vector> | ||
|
||
namespace CW { | ||
|
||
|
@@ -36,32 +37,31 @@ SUStringRef m_string; | |
String::String(): | ||
m_string(create_string_ref()), | ||
m_encoding(StringEncoding::UTF8) | ||
{ | ||
} | ||
{} | ||
|
||
|
||
String::String(SUStringRef string_ref): | ||
m_string(string_ref), | ||
m_encoding(StringEncoding::UTF8) | ||
{ | ||
} | ||
{} | ||
|
||
|
||
String::String(const std::string &string_input, StringEncoding enc): | ||
m_string(create_string_ref(string_input, enc)), | ||
m_encoding(enc) | ||
{ | ||
} | ||
{} | ||
|
||
|
||
String::String(const char string_input[]): | ||
m_string(create_string_ref(&string_input[0])), | ||
m_encoding(StringEncoding::UTF8) | ||
{ | ||
} | ||
{} | ||
|
||
|
||
String::String(const unichar string_input[]): | ||
m_string(create_string_ref(&string_input[0])), | ||
m_encoding(StringEncoding::UTF16) | ||
{ | ||
} | ||
{} | ||
|
||
|
||
String::String(const String& other): | ||
|
@@ -104,6 +104,7 @@ SUStringRef String::create_string_ref() { | |
return string_ref; | ||
} | ||
|
||
|
||
SUStringRef String::create_string_ref(std::string string_input, StringEncoding enc) { | ||
SUStringRef string_ref = SU_INVALID; | ||
if (enc == StringEncoding::UTF8) { | ||
|
@@ -118,19 +119,21 @@ SUStringRef String::create_string_ref(std::string string_input, StringEncoding e | |
return string_ref; | ||
} | ||
|
||
|
||
SUStringRef String::create_string_ref(const char string_input[]) { | ||
SUStringRef string_ref = SU_INVALID; | ||
SUStringCreateFromUTF8(&string_ref, &string_input[0]); | ||
return string_ref; | ||
} | ||
|
||
|
||
SUStringRef String::create_string_ref(const unichar string_input[]) { | ||
SUStringRef string_ref = SU_INVALID; | ||
// TODO UTF16 to be supported. | ||
//SUStringCreateFromUTF16(&string_ref, &string_input[0]); | ||
return string_ref; | ||
} | ||
|
||
|
||
String::~String() { | ||
if (SUIsValid(m_string)) { | ||
|
@@ -139,39 +142,38 @@ String::~String() { | |
} | ||
} | ||
|
||
|
||
SUStringRef String::ref() const { | ||
return m_string; | ||
} | ||
|
||
|
||
std::string String::std_string() const { | ||
size_t out_length = 0; | ||
SUResult res = SUStringGetUTF8Length(m_string, &out_length); | ||
assert(res == SU_ERROR_NONE); | ||
out_length++; // Allow for null termianted string | ||
char* char_array = new char[out_length]; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TommyKaneko
Author
Owner
|
||
res = SUStringGetUTF8(m_string, out_length, &char_array[0], &out_length); | ||
std::vector<char> char_array(out_length); | ||
res = SUStringGetUTF8(m_string, out_length, char_array.data(), &out_length); | ||
assert(res == SU_ERROR_NONE); | ||
std::string str(char_array); | ||
delete[] char_array; | ||
std::string str(char_array.begin(),char_array.end()); | ||
return str; | ||
} | ||
|
||
|
||
String::operator std::string() const { | ||
return std_string(); | ||
} | ||
|
||
//char& String::operator [](size_t i) { | ||
|
||
//} | ||
|
||
|
||
size_t String::size() const { | ||
size_t out_length = 0; | ||
SUResult res = SUStringGetUTF8Length(m_string, &out_length); | ||
assert(res == SU_ERROR_NONE); | ||
return out_length; | ||
} | ||
|
||
|
||
|
||
bool String::empty() const { | ||
// size of 1 is empty due to the \n character at the end. | ||
if (size() == 1) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,18 +180,16 @@ std::vector<std::string> AttributeDictionary::get_keys() const { | |
size_t num_keys = 0; | ||
SUResult res = SUAttributeDictionaryGetNumKeys(m_dict, &num_keys); | ||
assert(res == SU_ERROR_NONE); | ||
SUStringRef* keys_ref = new SUStringRef[num_keys]; | ||
for (size_t i=0; i < num_keys; i++) { | ||
keys_ref[i] = SU_INVALID; | ||
SUStringCreate(&keys_ref[i]); | ||
} | ||
SUAttributeDictionaryGetKeys(m_dict, num_keys, &keys_ref[0], &num_keys); | ||
std::vector<std::string> keys; | ||
keys.reserve(num_keys); | ||
for (size_t i=0; i < num_keys; i++) { | ||
keys.push_back(String(keys_ref[i])); | ||
} | ||
delete[] keys_ref; | ||
std::vector<SUStringRef> keys_ref(num_keys, SU_INVALID); | ||
//for (size_t i=0; i < num_keys; i++) { | ||
// SUStringCreate(&keys_ref[i]); | ||
This comment has been minimized.
Sorry, something went wrong.
thomthom
Contributor
|
||
//} | ||
SUAttributeDictionaryGetKeys(m_dict, num_keys, keys_ref.data(), &num_keys); | ||
std::vector<std::string> keys(num_keys); | ||
std::transform(keys_ref.begin(), keys_ref.end(), keys.begin(), | ||
[](const SUStringRef& value) { | ||
return String(value).std_string(); | ||
}); | ||
return keys; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,15 +64,14 @@ std::vector<Edge> Curve::get_edges() const{ | |
size_t num_edges = 0; | ||
SUResult res = SUCurveGetNumEdges(m_curve, &num_edges); | ||
assert(res == SU_ERROR_NONE); | ||
SUEdgeRef* ref_edges = new SUEdgeRef[num_edges]; | ||
res = SUCurveGetEdges(m_curve, num_edges, &ref_edges[0], &num_edges); | ||
std::vector<SUEdgeRef> ref_edges(num_edges); | ||
This comment has been minimized.
Sorry, something went wrong.
thomthom
Contributor
|
||
res = SUCurveGetEdges(m_curve, num_edges, ref_edges.data(), &num_edges); | ||
assert(res == SU_ERROR_NONE); | ||
std::vector<Edge> edges; | ||
edges.reserve(num_edges); | ||
for (size_t i=0; i < num_edges; ++i) { | ||
edges.push_back(Edge(ref_edges[i])); | ||
} | ||
delete ref_edges; | ||
std::vector<Edge> edges(num_edges); | ||
std::transform(ref_edges.begin(), ref_edges.end(), edges.begin(), | ||
[](const SUEdgeRef& value) { | ||
return Edge(value); | ||
}); | ||
return edges; | ||
} | ||
|
||
|
Might be worth explicitly filling it with NULL;
std::vector<char> char_array(out_length, 0);