Skip to content

Commit 60c281a

Browse files
committed
fix: assert size in remove_prefix/suffix
fix #973
1 parent e946887 commit 60c281a

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

include/boost/url/decode_view.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class decode_view
460460
461461
@par Preconditions
462462
@code
463-
not this->empty()
463+
n <= this->size()
464464
@endcode
465465
466466
@par Complexity
@@ -477,13 +477,13 @@ class decode_view
477477
@par Example
478478
@code
479479
decode_view d( "Program%20Files" );
480-
d.remove_prefix( 6 );
480+
d.remove_suffix( 6 );
481481
assert( d == "Program" );
482482
@endcode
483483
484484
@par Preconditions
485485
@code
486-
not this->empty()
486+
n <= this->size()
487487
@endcode
488488
489489
@par Complexity

src/decode_view.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void
5252
decode_view::
5353
remove_prefix( size_type n )
5454
{
55+
BOOST_ASSERT(n <= dn_);
5556
auto it = begin();
5657
auto n0 = n;
5758
while (n)
@@ -68,6 +69,7 @@ void
6869
decode_view::
6970
remove_suffix( size_type n )
7071
{
72+
BOOST_ASSERT(n <= dn_);
7173
auto it = end();
7274
auto n0 = n;
7375
while (n)

test/unit/decode_view.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,42 @@ struct decode_view_test
171171
BOOST_TEST_EQ(s, "uri test");
172172
}
173173

174+
// remove_prefix() with n == size() (issue #973)
175+
{
176+
decode_view s(str, no_plus_opt);
177+
s.remove_prefix(s.size());
178+
BOOST_TEST(s.empty());
179+
BOOST_TEST_EQ(s.size(), 0u);
180+
}
181+
182+
// remove_prefix() with n == 0
183+
{
184+
decode_view s(str, no_plus_opt);
185+
s.remove_prefix(0);
186+
BOOST_TEST_EQ(s, "a uri test");
187+
}
188+
174189
// remove_suffix()
175190
{
176191
decode_view s(str);
177192
s.remove_suffix(5);
178193
BOOST_TEST_EQ(s, "a uri");
179194
}
195+
196+
// remove_suffix() with n == size() (issue #973)
197+
{
198+
decode_view s(str, no_plus_opt);
199+
s.remove_suffix(s.size());
200+
BOOST_TEST(s.empty());
201+
BOOST_TEST_EQ(s.size(), 0u);
202+
}
203+
204+
// remove_suffix() with n == 0
205+
{
206+
decode_view s(str, no_plus_opt);
207+
s.remove_suffix(0);
208+
BOOST_TEST_EQ(s, "a uri test");
209+
}
180210
}
181211

182212
void
@@ -372,6 +402,78 @@ struct decode_view_test
372402
#endif
373403
}
374404

405+
void
406+
testJavadocs()
407+
{
408+
// decode_view()
409+
{
410+
decode_view ds;
411+
412+
boost::ignore_unused(ds);
413+
}
414+
415+
// decode_view(pct_string_view, encoding_opts)
416+
{
417+
decode_view ds( "Program%20Files" );
418+
419+
boost::ignore_unused(ds);
420+
}
421+
422+
// empty()
423+
{
424+
BOOST_TEST( decode_view( "" ).empty() );
425+
}
426+
427+
// size()
428+
{
429+
BOOST_TEST_EQ( decode_view( "Program%20Files" ).size(), 13u );
430+
}
431+
432+
// front()
433+
{
434+
BOOST_TEST_EQ( decode_view( "Program%20Files" ).front(), 'P' );
435+
}
436+
437+
// back()
438+
{
439+
BOOST_TEST_EQ( decode_view( "Program%20Files" ).back(), 's' );
440+
}
441+
442+
// starts_with(core::string_view)
443+
{
444+
BOOST_TEST( decode_view( "Program%20Files" ).starts_with("Program") );
445+
}
446+
447+
// ends_with(core::string_view)
448+
{
449+
BOOST_TEST( decode_view( "Program%20Files" ).ends_with("Files") );
450+
}
451+
452+
// starts_with(char)
453+
{
454+
BOOST_TEST( decode_view( "Program%20Files" ).starts_with('P') );
455+
}
456+
457+
// ends_with(char)
458+
{
459+
BOOST_TEST( decode_view( "Program%20Files" ).ends_with('s') );
460+
}
461+
462+
// remove_prefix(size_type)
463+
{
464+
decode_view d( "Program%20Files" );
465+
d.remove_prefix( 8 );
466+
BOOST_TEST_EQ( d, "Files" );
467+
}
468+
469+
// remove_suffix(size_type)
470+
{
471+
decode_view d( "Program%20Files" );
472+
d.remove_suffix( 6 );
473+
BOOST_TEST_EQ( d, "Program" );
474+
}
475+
}
476+
375477
void
376478
run()
377479
{
@@ -385,6 +487,7 @@ struct decode_view_test
385487
testStream();
386488
testPR127Cases();
387489
testBorrowedRange();
490+
testJavadocs();
388491
}
389492
};
390493

0 commit comments

Comments
 (0)