Skip to content

Commit ddef7ea

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

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

include/YY/Base/Strings/StringView.h

Lines changed: 102 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,32 @@ namespace YY
324348
TrimEnd(_sTrimChars);
325349
return *this;
326350
}
351+
352+
StringView __YYAPI SplitAndTakeFirst(_In_ char_t _chSplit) const
353+
{
354+
auto _uIndex = Find(_chSplit);
355+
if (_uIndex == size_t(-1))
356+
{
357+
return *this;
358+
}
359+
else
360+
{
361+
return Substring(0, _uIndex);
362+
}
363+
}
364+
365+
StringView __YYAPI SplitAndTakeFirst(_In_ StringView _sSplit) const
366+
{
367+
auto _uIndex = Find(_sSplit);
368+
if (_uIndex == size_t(-1))
369+
{
370+
return *this;
371+
}
372+
else
373+
{
374+
return Substring(0, _uIndex);
375+
}
376+
}
327377
};
328378

329379
template<>
@@ -529,7 +579,7 @@ namespace YY
529579
}
530580
}
531581

532-
size_t __YYAPI Find(char_t _ch, size_t _uIndex = 0) noexcept
582+
size_t __YYAPI Find(char_t _ch, size_t _uIndex = 0) const noexcept
533583
{
534584
for (; _uIndex < GetSize(); ++_uIndex)
535585
{
@@ -542,6 +592,30 @@ namespace YY
542592
return -1;
543593
}
544594

595+
size_t __YYAPI Find(StringView _sStr, size_t _uIndex = 0) const noexcept
596+
{
597+
if (_sStr.IsEmpty())
598+
return -1;
599+
600+
if (_uIndex + _sStr.GetLength() < GetSize())
601+
{
602+
return size_t(-1);
603+
}
604+
605+
auto _sStart = sString + _uIndex;
606+
const auto _sEnd = sString + GetSize() - _sStr.GetLength();
607+
const auto _cbCmp = _sStr.GetSize() * sizeof(_sStr[0]);
608+
for (; _sStart <= _sEnd; ++_sStart)
609+
{
610+
if (memcmp(_sStart, _sStr.GetConstString(), _cbCmp) == 0)
611+
{
612+
return _uIndex;
613+
}
614+
}
615+
616+
return -1;
617+
}
618+
545619
StringView& __YYAPI Slice(size_t _uRemoveStart, size_t _uRemoveEnd = 0u) noexcept
546620
{
547621
if (_uRemoveStart + _uRemoveEnd >= cchString)
@@ -622,6 +696,32 @@ namespace YY
622696
TrimEnd(_sTrimChars);
623697
return *this;
624698
}
699+
700+
StringView __YYAPI SplitAndTakeFirst(_In_ char_t _chSplit) const
701+
{
702+
auto _uIndex = Find(_chSplit);
703+
if (_uIndex == size_t(-1))
704+
{
705+
return *this;
706+
}
707+
else
708+
{
709+
return Substring(0, _uIndex);
710+
}
711+
}
712+
713+
StringView __YYAPI SplitAndTakeFirst(_In_ StringView _sSplit) const
714+
{
715+
auto _uIndex = Find(_sSplit);
716+
if (_uIndex == size_t(-1))
717+
{
718+
return *this;
719+
}
720+
else
721+
{
722+
return Substring(0, _uIndex);
723+
}
724+
}
625725
};
626726

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

0 commit comments

Comments
 (0)