summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Pipping <sebastian@pipping.org>2007-07-23 00:15:45 +0000
committerSebastian Pipping <sebastian@pipping.org>2007-07-23 00:15:45 +0000
commitf649f755c5f976834c01d24336c4de4a339ecba1 (patch)
tree02ec70126d80e44d67e3023a04c7b22a679d7274
parent44b7a1babfb7292d7fdf74fb1b8e46ebc9f37a6b (diff)
downloaduriparser-f649f755c5f976834c01d24336c4de4a339ecba1.tar.gz
Test and bugfix for ToStringCharsRequired
git-svn-id: https://uriparser.svn.sourceforge.net/svnroot/uriparser/uriparser/trunk@190 119732b9-2324-0410-a8a5-815c10060bbe
-rw-r--r--TODO.txt1
-rw-r--r--include/uriparser/Uri.h4
-rw-r--r--lib/Uri.c2
-rw-r--r--test/test.cpp51
4 files changed, 55 insertions, 3 deletions
diff --git a/TODO.txt b/TODO.txt
index f524130..b5c07bd 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -1,5 +1,4 @@
== BEFORE NEXT RELEASE ==
- * Test ToStringCharsRequired
== SOON ==
* More features:
diff --git a/include/uriparser/Uri.h b/include/uriparser/Uri.h
index f29ca0a..cd3bfd3 100644
--- a/include/uriparser/Uri.h
+++ b/include/uriparser/Uri.h
@@ -262,7 +262,7 @@ UriBool URI_FUNC(EqualsUri)(const URI_TYPE(Uri) * a, const URI_TYPE(Uri) * b);
* terminator.
*
* @param uri %URI to measure
- * @param charsRequired Length of the string representation in characters
+ * @param charsRequired Length of the string representation in characters <u>excluding</u> terminator
* @return Error code or 0 on success
*/
int URI_FUNC(ToStringCharsRequired)(const URI_TYPE(Uri) * uri,
@@ -276,7 +276,7 @@ int URI_FUNC(ToStringCharsRequired)(const URI_TYPE(Uri) * uri,
*
* @param dest Output destination
* @param uri %URI to convert
- * @param maxChars Maximum number of characters to copy including terminator
+ * @param maxChars Maximum number of characters to copy <u>including</u> terminator
* @param charsWritten Number of characters written, can be lower than maxChars even if the %URI is too long!
* @return Error code or 0 on success
*/
diff --git a/lib/Uri.c b/lib/Uri.c
index 6e177cf..80733ed 100644
--- a/lib/Uri.c
+++ b/lib/Uri.c
@@ -3926,6 +3926,8 @@ static URI_INLINE int URI_FUNC(ToStringEngine)(URI_CHAR * dest, const URI_TYPE(U
/* [01/19] result = "" */
if (dest != NULL) {
dest[0] = _UT('\0');
+ } else {
+ (*charsRequired) = 0;
}
/* [02/19] if defined(scheme) then */
if (uri->scheme.first != NULL) {
diff --git a/test/test.cpp b/test/test.cpp
index 553a1e6..7d4c28e 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -72,6 +72,7 @@ public:
TEST_ADD(UriSuite::testTrailingSlash)
TEST_ADD(UriSuite::testAddBase)
TEST_ADD(UriSuite::testToString)
+ TEST_ADD(UriSuite::testToStringCharsRequired)
}
private:
@@ -807,6 +808,56 @@ private:
TEST_ASSERT(testToStringHelper(L"?query#fragment"));
}
+ bool testToStringCharsRequiredHelper(wchar_t * text) {
+ // Parse
+ UriParserStateW state;
+ UriUriW uri;
+ state.uri = &uri;
+ int res = uriParseUriW(&state, text);
+ if (res != 0) {
+ uriFreeUriMembersW(&uri);
+ return false;
+ }
+
+ // Required space?
+ int charsRequired;
+ if (uriToStringCharsRequiredW(&uri, &charsRequired) != 0) {
+ uriFreeUriMembersW(&uri);
+ return false;
+ }
+
+ // Minimum
+ wchar_t * buffer = new wchar_t[charsRequired + 1];
+ if (uriToStringW(buffer, &uri, charsRequired + 1, NULL) != 0) {
+ uriFreeUriMembersW(&uri);
+ delete [] buffer;
+ return false;
+ }
+
+ // One less than minimum
+ if (uriToStringW(buffer, &uri, charsRequired, NULL) == 0) {
+ uriFreeUriMembersW(&uri);
+ delete [] buffer;
+ return false;
+ }
+
+ uriFreeUriMembersW(&uri);
+ delete [] buffer;
+ return true;
+ }
+
+ void testToStringCharsRequired() {
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://www.example.com/"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://www.example.com:80/"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://user:pass@www.example.com/"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://www.example.com/index.html"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://www.example.com/?abc"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://www.example.com/#def"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"http://www.example.com/?abc#def"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"/test"));
+ TEST_ASSERT(testToStringCharsRequiredHelper(L"test"));
+ }
+
};