Skip to content

Commit 9352d32

Browse files
Fea, 添加StringView::SplitAndTakeFirst
1 parent 9eba0f5 commit 9352d32

File tree

1 file changed

+150
-2
lines changed

1 file changed

+150
-2
lines changed

include/YY/Base/Strings/StringView.h

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ namespace YY
228228
}
229229
}
230230

231-
size_t __YYAPI Find(char_t _ch, size_t _uIndex = 0) noexcept
231+
size_t __YYAPI Find(char_t _ch, size_t _uIndex = 0) const noexcept
232232
{
233233
for (; _uIndex < GetSize(); ++_uIndex)
234234
{
@@ -241,6 +241,30 @@ namespace YY
241241
return -1;
242242
}
243243

244+
size_t __YYAPI Find(StringView _sStr, size_t _uIndex = 0) const noexcept
245+
{
246+
if (_sStr.IsEmpty())
247+
return -1;
248+
249+
if (_uIndex + _sStr.GetLength() < GetSize())
250+
{
251+
return size_t(-1);
252+
}
253+
254+
auto _sStart = sString + _uIndex;
255+
const auto _sEnd = sString + GetSize() - _sStr.GetLength();
256+
const auto _cbCmp = _sStr.GetSize() * sizeof(_sStr[0]);
257+
for (; _sStart <= _sEnd; ++_sStart)
258+
{
259+
if (memcmp(_sStart, _sStr.GetConstString(), _cbCmp) == 0)
260+
{
261+
return _uIndex;
262+
}
263+
}
264+
265+
return -1;
266+
}
267+
244268
StringView& __YYAPI Slice(size_t _uRemoveStart, size_t _uRemoveEnd = 0u) noexcept
245269
{
246270
if (_uRemoveStart + _uRemoveEnd >= cchString)
@@ -324,6 +348,56 @@ namespace YY
324348
TrimEnd(_sTrimChars);
325349
return *this;
326350
}
351+
352+
StringView __YYAPI SplitAndTakeFirst(_In_ char_t _chSplit, _Out_opt_ StringView* _psRemaining = nullptr) const
353+
{
354+
auto _uIndex = Find(_chSplit);
355+
if (_uIndex == size_t(-1))
356+
{
357+
StringView _sResult = *this;
358+
if (_psRemaining)
359+
{
360+
*_psRemaining = StringView();
361+
}
362+
363+
return _sResult;
364+
}
365+
else
366+
{
367+
StringView _sResult = Substring(0, _uIndex);;
368+
if (_psRemaining)
369+
{
370+
*_psRemaining = Substring(_uIndex + 1);
371+
}
372+
373+
return _sResult;
374+
}
375+
}
376+
377+
StringView __YYAPI SplitAndTakeFirst(_In_ StringView _sSplit, _Out_opt_ StringView* _psRemaining = nullptr) const
378+
{
379+
auto _uIndex = Find(_sSplit);
380+
if (_uIndex == size_t(-1))
381+
{
382+
StringView _sResult = *this;
383+
if (_psRemaining)
384+
{
385+
*_psRemaining = StringView();
386+
}
387+
388+
return _sResult;
389+
}
390+
else
391+
{
392+
StringView _sResult = Substring(0, _uIndex);;
393+
if (_psRemaining)
394+
{
395+
*_psRemaining = Substring(_uIndex + _sSplit.GetLength());
396+
}
397+
398+
return _sResult;
399+
}
400+
}
327401
};
328402

329403
template<>
@@ -529,7 +603,7 @@ namespace YY
529603
}
530604
}
531605

532-
size_t __YYAPI Find(char_t _ch, size_t _uIndex = 0) noexcept
606+
size_t __YYAPI Find(char_t _ch, size_t _uIndex = 0) const noexcept
533607
{
534608
for (; _uIndex < GetSize(); ++_uIndex)
535609
{
@@ -542,6 +616,30 @@ namespace YY
542616
return -1;
543617
}
544618

619+
size_t __YYAPI Find(StringView _sStr, size_t _uIndex = 0) const noexcept
620+
{
621+
if (_sStr.IsEmpty())
622+
return -1;
623+
624+
if (_uIndex + _sStr.GetLength() < GetSize())
625+
{
626+
return size_t(-1);
627+
}
628+
629+
auto _sStart = sString + _uIndex;
630+
const auto _sEnd = sString + GetSize() - _sStr.GetLength();
631+
const auto _cbCmp = _sStr.GetSize() * sizeof(_sStr[0]);
632+
for (; _sStart <= _sEnd; ++_sStart)
633+
{
634+
if (memcmp(_sStart, _sStr.GetConstString(), _cbCmp) == 0)
635+
{
636+
return _uIndex;
637+
}
638+
}
639+
640+
return -1;
641+
}
642+
545643
StringView& __YYAPI Slice(size_t _uRemoveStart, size_t _uRemoveEnd = 0u) noexcept
546644
{
547645
if (_uRemoveStart + _uRemoveEnd >= cchString)
@@ -622,6 +720,56 @@ namespace YY
622720
TrimEnd(_sTrimChars);
623721
return *this;
624722
}
723+
724+
StringView __YYAPI SplitAndTakeFirst(_In_ char_t _chSplit, _Out_opt_ StringView* _psRemaining = nullptr) const
725+
{
726+
auto _uIndex = Find(_chSplit);
727+
if (_uIndex == size_t(-1))
728+
{
729+
StringView _sResult = *this;
730+
if (_psRemaining)
731+
{
732+
*_psRemaining = StringView();
733+
}
734+
735+
return _sResult;
736+
}
737+
else
738+
{
739+
StringView _sResult = Substring(0, _uIndex);;
740+
if (_psRemaining)
741+
{
742+
*_psRemaining = Substring(_uIndex + 1);
743+
}
744+
745+
return _sResult;
746+
}
747+
}
748+
749+
StringView __YYAPI SplitAndTakeFirst(_In_ StringView _sSplit, _Out_opt_ StringView* _psRemaining = nullptr) const
750+
{
751+
auto _uIndex = Find(_sSplit);
752+
if (_uIndex == size_t(-1))
753+
{
754+
StringView _sResult = *this;
755+
if (_psRemaining)
756+
{
757+
*_psRemaining = StringView();
758+
}
759+
760+
return _sResult;
761+
}
762+
else
763+
{
764+
StringView _sResult = Substring(0, _uIndex);;
765+
if (_psRemaining)
766+
{
767+
*_psRemaining = Substring(_uIndex + _sSplit.GetLength());
768+
}
769+
770+
return _sResult;
771+
}
772+
}
625773
};
626774

627775
typedef StringView<YY::Base::achar_t, Encoding::ANSI> aStringView;

0 commit comments

Comments
 (0)