Debian Codegraph @

LspCpp/include/LibLsp/lsp/Markup/string_ref.h

raw
1
2 #pragma once
3 #include <algorithm>
4 #include <string>
5 #include <vector>
6 #include <stdarg.h>
7 #include <functional>
8
9 #ifndef _WIN32
10 #include <cstring>
11 #endif
12 namespace std
13 {
14
15 /**
16 * An extension of STL's string providing additional functionality that is often availiable in
17 * higher-level languages such as Python.
18 */
19 class string_ref : public string
20 {
21 public:
22 //static unsigned GetAutoSenseRadix(string_ref& Str) {
23 // if (Str.empty())
24 // return 10;
25
26 // if (Str.start_with("0x") || Str.start_with("0X")) {
27 // Str = Str.substr(2);
28 // return 16;
29 // }
30
31 // if (Str.start_with("0b") || Str.start_with("0B")) {
32 // Str = Str.substr(2);
33 // return 2;
34 // }
35
36 // if (Str.start_with("0o")) {
37 // Str = Str.substr(2);
38 // return 8;
39 // }
40
41 // if (Str[0] == '0' && Str.size() > 1 && std::isdigit(Str[1])) {
42 // Str = Str.substr(1);
43 // return 8;
44 // }
45
46 // return 10;
47 //}
48
49 //static bool consumeUnsignedInteger(string_ref& Str, unsigned Radix,
50 // unsigned long long& Result) {
51 // // Autosense radix if not specified.
52 // if (Radix == 0)
53 // Radix = GetAutoSenseRadix(Str);
54
55 // // Empty strings (after the radix autosense) are invalid.
56 // if (Str.empty()) return true;
57
58 // // Parse all the bytes of the string given this radix. Watch for overflow.
59 // string_ref Str2 = Str;
60 // Result = 0;
61 // while (!Str2.empty()) {
62 // unsigned CharVal;
63 // if (Str2[0] >= '0' && Str2[0] <= '9')
64 // CharVal = Str2[0] - '0';
65 // else if (Str2[0] >= 'a' && Str2[0] <= 'z')
66 // CharVal = Str2[0] - 'a' + 10;
67 // else if (Str2[0] >= 'A' && Str2[0] <= 'Z')
68 // CharVal = Str2[0] - 'A' + 10;
69 // else
70 // break;
71
72 // // If the parsed value is larger than the integer radix, we cannot
73 // // consume any more characters.
74 // if (CharVal >= Radix)
75 // break;
76
77 // // Add in this character.
78 // unsigned long long PrevResult = Result;
79 // Result = Result * Radix + CharVal;
80
81 // // Check for overflow by shifting back and seeing if bits were lost.
82 // if (Result / Radix < PrevResult)
83 // return true;
84
85 // Str2 = Str2.substr(1);
86 // }
87
88 // // We consider the operation a failure if no characters were consumed
89 // // successfully.
90 // if (Str.size() == Str2.size())
91 // return true;
92
93 // Str = Str2;
94 // return false;
95 //}
96
97 //static bool consumeSignedInteger(string_ref& Str, unsigned Radix,
98 // long long& Result) {
99 // unsigned long long ULLVal;
100
101 // // Handle positive strings first.
102 // if (Str.empty() || Str.front() != '-') {
103 // if (consumeUnsignedInteger(Str, Radix, ULLVal) ||
104 // // Check for value so large it overflows a signed value.
105 // (long long)ULLVal < 0)
106 // return true;
107 // Result = ULLVal;
108 // return false;
109 // }
110
111 // // Get the positive part of the value.
112 // string_ref Str2 = Str.drop_front(1);
113 // if (consumeUnsignedInteger(Str2, Radix, ULLVal) ||
114 // // Reject values so large they'd overflow as negative signed, but allow
115 // // "-0". This negates the unsigned so that the negative isn't undefined
116 // // on signed overflow.
117 // (long long)-ULLVal > 0)
118 // return true;
119
120 // Str = Str2;
121 // Result = -ULLVal;
122 // return false;
123 //}
124
125 ///// GetAsUnsignedInteger - Workhorse method that converts a integer character
126 ///// sequence of radix up to 36 to an unsigned long long value.
127 //static bool getAsUnsignedInteger(string_ref Str, unsigned Radix,
128 // unsigned long long& Result) {
129 // if (consumeUnsignedInteger(Str, Radix, Result))
130 // return true;
131
132 // // For getAsUnsignedInteger, we require the whole string to be consumed or
133 // // else we consider it a failure.
134 // return !Str.empty();
135 //}
136
137 //static bool getAsSignedInteger(string_ref Str, unsigned Radix,
138 // long long& Result) {
139 // if (consumeSignedInteger(Str, Radix, Result))
140 // return true;
141
142 // // For getAsSignedInteger, we require the whole string to be consumed or else
143 // // we consider it a failure.
144 // return !Str.empty();
145 //}
146
147 ///// Parse the current string as an integer of the specified radix. If
148 ///// \p Radix is specified as zero, this does radix autosensing using
149 ///// extended C rules: 0 is octal, 0x is hex, 0b is binary.
150 /////
151 ///// If the string is invalid or if only a subset of the string is valid,
152 ///// this returns true to signify the error. The string is considered
153 ///// erroneous if empty or if it overflows T.
154 //template <typename T>
155 //std::enable_if_t<std::numeric_limits<T>::is_signed, bool>
156 // getAsInteger(unsigned Radix, T& Result) const {
157 // long long LLVal;
158 // if (getAsSignedInteger(*this, Radix, LLVal) ||
159 // static_cast<T>(LLVal) != LLVal)
160 // return true;
161 // Result = LLVal;
162 // return false;
163 //}
164
165 //template <typename T>
166 //std::enable_if_t<!std::numeric_limits<T>::is_signed, bool>
167 // getAsInteger(unsigned Radix, T& Result) const {
168 // unsigned long long ULLVal;
169 // // The additional cast to unsigned long long is required to avoid the
170 // // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
171 // // 'unsigned __int64' when instantiating getAsInteger with T = bool.
172 // if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
173 // static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
174 // return true;
175 // Result = ULLVal;
176 // return false;
177 //}
178
179 /**`
180 * Default constructor
181 *
182 * Constructs an empty string_ref ("")
183 */
184 string_ref() : string()
185 {
186 }
187
188 /**
189 * Duplicate the STL string copy constructor
190 *
191 * @param[in] s The string to copy
192 * @param[in] pos The starting position in the string to copy from
193 * @param[in] n The number of characters to copy
194 */
195 string_ref(string const& s, size_type pos = 0, size_type n = npos) : string(s, pos, n)
196 {
197 }
198
199 /**
200 * Construct an string_ref from a null-terminated character array
201 *
202 * @param[in] s The character array to copy into the new string
203 */
204 string_ref(value_type const* s) : string(s)
205 {
206 }
207
208 /**
209 * Construct an string_ref from a character array and a length
210 *
211 * @param[in] s The character array to copy into the new string
212 * @param[in] n The number of characters to copy
213 */
214 string_ref(value_type const* s, size_type n) : string(s, n)
215 {
216 }
217
218 /**
219 * Create an string_ref with @p n copies of @p c
220 *
221 * @param[in] n The number of copies
222 * @param[in] c The character to copy @p n times
223 */
224 string_ref(size_type n, value_type c) : string(n, c)
225 {
226 }
227
228 /**
229 * Create a string from a range
230 *
231 * @param[in] first The first element to copy in
232 * @param[in] last The last element to copy in
233 */
234 template<class InputIterator>
235 string_ref(InputIterator first, InputIterator last) : string(first, last)
236 {
237 }
238
239 /**
240 * The destructor
241 */
242 ~string_ref()
243 {
244 }
245
246 /**
247 * Split a string by whitespace
248 *
249 * @return A vector of strings, each of which is a substring of the string
250 */
251 vector<string_ref> split(size_type limit = npos) const
252 {
253 vector<string_ref> v;
254
255 const_iterator i = begin(), last = i;
256 for (; i != end(); i++)
257 {
258 if (*i == ' ' || *i == '\n' || *i == '\t' || *i == '\r')
259 {
260 if (i + 1 != end() && (i[1] == ' ' || i[1] == '\n' || i[1] == '\t' || i[1] == '\r'))
261 {
262 continue;
263 }
264 v.push_back(string_ref(last, i));
265 last = i + 1;
266 if (v.size() >= limit - 1)
267 {
268 v.push_back(string_ref(last, end()));
269 return v;
270 }
271 }
272 }
273
274 if (last != i)
275 {
276 v.push_back(string_ref(last, i));
277 }
278
279 return v;
280 }
281
282 /**
283 * Split a string by a character
284 *
285 * Returns a vector of ext_strings, each of which is a substring of the string formed by splitting
286 * it on boundaries formed by the character @p separator. If @p limit is set, the returned vector
287 * will contain a maximum of @p limit elements with the last element containing the rest of
288 * the string.
289 *
290 * If @p separator is not found in the string, a single element will be returned in the vector
291 * containing the entire string.
292 *
293 * The separators are removed from the output
294 *
295 * @param[in] separator The character separator to split the string on
296 * @param[in] limit The maximum number of output elements
297 * @return A vector of strings, each of which is a substring of the string
298 *
299 * @section split_ex Example
300 * @code
301 * std::string_ref s("This|is|a|test.");
302 * std::vector<std::string_ref> v = s.split('|');
303 * std::copy(v.begin(), v.end(), std::ostream_iterator<std::string_ref>(std::cout, "\n"));
304 *
305 * This
306 * is
307 * a
308 * test.
309 * @endcode
310 */
311 vector<string_ref> split(value_type separator, size_type limit = npos) const
312 {
313 vector<string_ref> v;
314
315 const_iterator i = begin(), last = i;
316 for (; i != end(); i++)
317 {
318 if (*i == separator)
319 {
320 v.push_back(string_ref(last, i));
321 last = i + 1;
322 if (v.size() >= limit - 1)
323 {
324 v.push_back(string_ref(last, end()));
325 return v;
326 }
327 }
328 }
329
330 if (last != i)
331 {
332 v.push_back(string_ref(last, i));
333 }
334
335 return v;
336 }
337
338 /**
339 * Split a string by another string
340 *
341 * Returns a vector of ext_strings, each of which is a substring of the string formed by
342 * splitting it on boundaries formed by the string @p separator. If @p limit is set, the
343 * returned vector will contain a maximum of @p limit elements with the last element
344 * containing the rest of the string.
345 *
346 * If @p separator is not found in the string, a single element will be returned in the
347 * vector containing the entire string.
348 *
349 * The separators are removed from the output
350 *
351 * @param[in] separator The string separator to split the string on
352 * @param[in] limit The maximum number of output elements
353 * @return A vector of strings, each of which is a substring of the string
354 *
355 * @ref split_ex
356 */
357 vector<string_ref> split(string const& separator, size_type limit = npos) const
358 {
359 vector<string_ref> v;
360
361 const_iterator i = begin(), last = i;
362 for (; i != end(); i++)
363 {
364 if (string(i, i + separator.length()) == separator)
365 {
366 v.push_back(string_ref(last, i));
367 last = i + separator.length();
368
369 if (v.size() >= limit - 1)
370 {
371 v.push_back(string_ref(last, end()));
372 return v;
373 }
374 }
375 }
376
377 if (last != i)
378 {
379 v.push_back(string_ref(last, i));
380 }
381
382 return v;
383 }
384
385 /**
386 * Convert a string into an integer
387 *
388 * Convert the initial portion of a string into a signed integer. Once a non-numeric
389 * character is reached, the remainder of @p string is ignored and the integer that was
390 * read returned.
391 *
392 * @param s The string to convert
393 * @return The integer converted from @p string
394 */
395 static long int integer(string const& s)
396 {
397 long int retval = 0;
398 bool neg = false;
399
400 for (const_iterator i = s.begin(); i != s.end(); i++)
401 {
402 if (i == s.begin())
403 {
404 if (*i == '-')
405 {
406 neg = true;
407 continue;
408 }
409 else if (*i == '+')
410 {
411 continue;
412 }
413 }
414 if (*i >= '0' && *i <= '9')
415 {
416 retval *= 10;
417 retval += *i - '0';
418 }
419 else
420 {
421 break;
422 }
423 }
424
425 if (neg)
426 {
427 retval *= -1;
428 }
429
430 return retval;
431 }
432
433 /**
434 * Convert the string to an integer
435 *
436 * Convert the initial portion of the string into a signed integer. Once a non-numeric
437 * character is reached, the remainder of the string is ignored and the integer that had
438 * been read thus far is returned.
439 *
440 * @return The integer converted from the string
441 */
442 long int integer() const
443 {
444 return integer(*this);
445 }
446
447 /**
448 * Split a string into chunks of size @p chunklen. Returns a vector of strings.
449 *
450 * Splits a string into chunks of the given size. The final chunk may not fill its
451 * entire allocated number of characters.
452 *
453 * @param[in] chunklen The number of characters per chunk
454 * @return A vector of strings, each of length <= chunklen
455 *
456 * @section chunk_split-ex Example
457 * @code
458 * std::string_ref s("abcdefghijk");
459 * std::vector<std::string_ref> v = s.chunk_split(3);
460 * std::copy(v.begin(), v.end(), ostream_iterator<std::string_ref>(cout, " "));
461 *
462 * abc def ghi jk
463 * @endcode
464 */
465 vector<string_ref> chunk_split(size_type chunklen) const
466 {
467 vector<string_ref> retval;
468 retval.reserve(size() / chunklen + 1);
469
470 size_type count = 0;
471 const_iterator i = begin(), last = i;
472 for (; i != end(); i++, count++)
473 {
474 if (count == chunklen)
475 {
476 count = 0;
477 retval.push_back(string_ref(last, i));
478 last = i;
479 }
480 }
481
482 if (last != i)
483 {
484 retval.push_back(string_ref(last, i));
485 }
486
487 return retval;
488 }
489
490 /**
491 * Join a sequence of strings by some glue to create a new string
492 *
493 * Glue is not added to the end of the string.
494 *
495 * @pre [first, last) is a valid range
496 * @pre InputIterator is a model of STL's Input Iterator
497 * @pre InputIterator must point to a string type (std::string, std::string_ref, char *)
498 *
499 * @param[in] glue The glue to join strings with
500 * @param[in] first The beginning of the range to join
501 * @param[in] last The end of the range to join
502 * @return A string constructed of each element of the range connected together with @p glue
503 *
504 * @section join_ex Example
505 * @code
506 * std::vector<std::string_ref> v;
507 * v.push_back("This");
508 * v.push_back("is");
509 * v.push_back("a");
510 * v.push_back("test.");
511 * std::cout << std::string_ref::join("|", v.begin(), v.end()) << std::endl;
512 *
513 * This|is|a|test.
514 * @endcode
515 */
516 template<class InputIterator>
517 static string_ref join(string const& glue, InputIterator first, InputIterator last)
518 {
519 string_ref retval;
520
521 for (; first != last; ++first)
522 {
523 retval.append(*first);
524 retval.append(glue);
525 }
526 retval.erase(retval.length() - glue.length());
527
528 return retval;
529 }
530
531 /**
532 * Join a sequence of strings by some glue to create a new string
533 *
534 * @copydoc join
535 * @ref join_ex
536 */
537 template<class InputIterator>
538 static string_ref join(value_type glue, InputIterator first, InputIterator last)
539 {
540 string_ref retval;
541
542 for (; first != last; ++first)
543 {
544 retval.append(*first);
545 retval.append(1, glue);
546 }
547 retval.erase(retval.length() - 1);
548
549 return retval;
550 }
551
552 /**
553 * Search for any instances of @p needle and replace them with @p s
554 *
555 * @param[in] needle The string to replace
556 * @param[in] s The replacement string
557 * @return *this
558 * @post All instances of @p needle in the string are replaced with @p s
559 *
560 * @section replace-ex Example
561 * @code
562 * std::string_ref s("This is a test.");
563 * s.replace("is", "ere");
564 * std::cout << s << std::endl;
565 *
566 * There ere a test.
567 * @endcode
568 */
569 string_ref& replace(string const& needle, string const& s)
570 {
571 size_type lastpos = 0, thispos;
572
573 while ((thispos = find(needle, lastpos)) != npos)
574 {
575 string::replace(thispos, needle.length(), s);
576 lastpos = thispos + 1;
577 }
578 return *this;
579 }
580 string_ref& replace_first(string const& needle, string const& s)
581 {
582 size_type lastpos = 0, thispos;
583
584 if ((thispos = find(needle, lastpos)) != npos)
585 {
586 string::replace(thispos, needle.length(), s);
587 lastpos = thispos + 1;
588 }
589 return *this;
590 }
591 /**
592 * Search of any instances of @p needle and replace them with @p c
593 *
594 * @param[in] needle The character to replace
595 * @param[in] c The replacement character
596 * @return *this
597 * @post All instances of @p needle in the string are replaced with @p c
598 *
599 * @ref replace-ex
600 */
601 string_ref& replace(value_type needle, value_type c)
602 {
603 for (iterator i = begin(); i != end(); i++)
604 {
605 if (*i == needle)
606 {
607 *i = c;
608 }
609 }
610
611 return *this;
612 }
613
614 /**
615 * Repeat a string @p n times
616 *
617 * @param[in] n The number of times to repeat the string
618 * @return string_ref containing @p n copies of the string
619 *
620 * @section repeat-ex Example
621 * @code
622 * std::string_ref s("123");
623 * s = s * 3;
624 * std::cout << s << std::endl;
625 *
626 * 123123123
627 * @endcode
628 */
629 string_ref operator*(size_type n)
630 {
631 string_ref retval;
632 for (size_type i = 0; i < n; i++)
633 {
634 retval.append(*this);
635 }
636
637 return retval;
638 }
639
640 /**
641 * Convert the string to lowercase
642 *
643 * @return *this
644 * @post The string is converted to lowercase
645 */
646 string_ref& tolower()
647 {
648 for (iterator i = begin(); i != end(); i++)
649 {
650 if (*i >= 'A' && *i <= 'Z')
651 {
652 *i = (*i) + ('a' - 'A');
653 }
654 }
655 return *this;
656 }
657
658 /**
659 * Convert the string to uppercase
660 *
661 * @return *this
662 * @post The string is converted to uppercase
663 */
664 string_ref& toupper()
665 {
666 for (iterator i = begin(); i != end(); i++)
667 {
668 if (*i >= 'a' && *i <= 'z')
669 {
670 *i = (*i) - ('a' - 'A');
671 }
672 }
673 return *this;
674 }
675
676 /**
677 * Count the occurances of @p str in the string.
678 *
679 * @return The count of substrings @p str in the string
680 */
681 size_type count(string const& str) const
682 {
683 size_type count = 0, last = 0, cur = 0;
684
685 while ((cur = find(str, last + 1)) != npos)
686 {
687 count++;
688 last = cur;
689 }
690
691 return count;
692 }
693
694 /**
695 * Determine if the string is alphanumeric
696 *
697 * @return true if the string contains only characters between a-z, A-Z and 0-9 and
698 * contains at least one character, else false
699 */
700 bool is_alnum() const
701 {
702 if (length() == 0)
703 {
704 return false;
705 }
706
707 for (const_iterator i = begin(); i != end(); i++)
708 {
709 if (*i < 'A' || *i > 'Z')
710 {
711 if (*i < '0' || *i > '9')
712 {
713 if (*i < 'a' || *i > 'z')
714 {
715 return false;
716 }
717 }
718 }
719 }
720
721 return true;
722 }
723
724 /**
725 * Determine if the string is alphabetic only
726 *
727 * @return true of the string contains only characters between a-z and A-Z and contains at
728 * least one character, else false
729 */
730 bool is_alpha() const
731 {
732 if (length() == 0)
733 {
734 return false;
735 }
736
737 for (const_iterator i = begin(); i != end(); i++)
738 {
739 if (*i < 'A' || (*i > 'Z' && (*i < 'a' || *i > 'z')))
740 {
741 return false;
742 }
743 }
744
745 return true;
746 }
747
748 /**
749 * Determine if the string is numeric only
750 *
751 * @return true if the string contains only characters between 0-9 and contains at least
752 * one character, else false
753 */
754 bool is_numeric() const
755 {
756 if (length() == 0)
757 {
758 return false;
759 }
760
761 for (const_iterator i = begin(); i != end(); i++)
762 {
763 if (*i < '0' || *i > '9')
764 {
765 return false;
766 }
767 }
768
769 return true;
770 }
771
772 /**
773 * Determine if a string is all lower case
774 *
775 * @return true if there is at least one character, and all characters are lowercase
776 * letters, else false
777 */
778 bool is_lower() const
779 {
780 if (length() == 0)
781 {
782 return false;
783 }
784
785 for (const_iterator i = begin(); i != end(); i++)
786 {
787 if (*i < 'a' || *i < 'z')
788 {
789 return false;
790 }
791 }
792
793 return true;
794 }
795
796 /**
797 * Determine if a string is all upper case
798 *
799 * @return true if there is at least one character, and all characters are uppercase
800 * letters, else false
801 */
802 bool is_upper() const
803 {
804 if (length() == 0)
805 {
806 return false;
807 }
808
809 for (const_iterator i = begin(); i != end(); i++)
810 {
811 if (*i < 'A' || *i > 'Z')
812 {
813 return false;
814 }
815 }
816
817 return true;
818 }
819
820 /**
821 * Swap the case of a string
822 *
823 * @post Converts all uppercase to lowercase, and all lowercase to uppercase in the string
824 * @return *this
825 */
826 string_ref& swapcase()
827 {
828 for (iterator i = begin(); i != end(); i++)
829 {
830 if (*i >= 'A' && *i <= 'Z')
831 {
832 *i += ('a' - 'A');
833 }
834 else if (*i >= 'a' && *i <= 'z')
835 {
836 *i -= ('a' - 'A');
837 }
838 }
839
840 return *this;
841 }
842
843 /*******************************************************************************
844 Function: std::string_ref::start_with
845 Access: public
846 Qualifier: const
847 Parameter: const string & str
848 Returns: bool
849
850 Purpose: is the string start with str
851 *******************************************************************************/
852 bool start_with(string const& str) const
853 {
854 return (this->find(str) == 0);
855 }
856
857 /// Return a string_ref equal to 'this' but with only the last \p N
858 /// elements remaining. If \p N is greater than the length of the
859 /// string, the entire string is returned.
860
861 string_ref take_back(size_t N = 1) const
862 {
863 if (N >= size())
864 {
865 return *this;
866 }
867 return drop_front(size() - N);
868 }
869 /// Return a string_ref equal to 'this' but with the first \p N elements
870 /// dropped.
871
872 string_ref drop_front(size_t N = 1) const
873 {
874 //assert(size() >= N && "Dropping more elements than exist");
875 return substr(N);
876 }
877
878 /// Return a string_ref equal to 'this' but with the last \p N elements
879 /// dropped.
880
881 string_ref drop_back(size_t N = 1) const
882 {
883
884 return substr(0, size() - N);
885 }
886
887 /// Return a string_ref equal to 'this', but with all characters satisfying
888 /// the given predicate dropped from the beginning of the string.
889
890 string_ref drop_while(std::function<bool(char)> F) const
891 {
892 return substr(std::find_if_not(begin(), end(), F) - begin());
893 }
894
895 /// Return a string_ref equal to 'this', but with all characters not
896 /// satisfying the given predicate dropped from the beginning of the string.
897
898 string_ref drop_until(std::function<bool(char)> F) const
899 {
900 return substr(std::find_if(begin(), end(), F) - begin());
901 }
902
903 /// Returns true if this string_ref has the given prefix and removes that
904 /// prefix.
905 bool consume_front(string_ref Prefix)
906 {
907 if (!start_with(Prefix))
908 {
909 return false;
910 }
911
912 *this = drop_front(Prefix.size());
913 return true;
914 }
915
916 /// Returns true if this string_ref has the given suffix and removes that
917 /// suffix.
918 bool consume_back(string_ref Suffix)
919 {
920 if (!end_with(Suffix))
921 {
922 return false;
923 }
924
925 *this = drop_back(Suffix.size());
926 return true;
927 }
928
929 /*******************************************************************************
930 Function: std::string_ref::end_with
931 Access: public
932 Qualifier: const
933 Parameter: const string & str
934 Returns: bool
935
936 Purpose: is the string end with str
937 *******************************************************************************/
938 bool end_with(string const& str) const
939 {
940 if (str.length() > this->length())
941 {
942 return false;
943 }
944 size_type off = this->length() - str.length();
945 return (find(str, off) == off);
946 }
947
948 /*******************************************************************************
949 Function: hl_lib::string_ref::format
950 Access: public
951 Qualifier:
952 Parameter: const char * format_string
953 Parameter: ...
954 Returns: string_ref&
955
956 Purpose: format the string
957 *******************************************************************************/
958 string_ref& format(char const* format_string, ...)
959 {
960 if (format_string == 0)
961 {
962 return *this;
963 }
964
965 va_list argList;
966 va_start(argList, format_string);
967
968 #ifdef _WIN32
969 int len = _vscprintf(format_string, argList);
970 char* pbuf = new char[len + 1];
971 if (pbuf != 0)
972 {
973 vsprintf_s(pbuf, len + 1, format_string, argList);
974 *this = pbuf;
975 delete[] pbuf;
976 }
977 #else
978 int const INLINE_FORMAT_BUFFER_LEN = 2048;
979 char* buf = new char[INLINE_FORMAT_BUFFER_LEN + 1];
980 if (buf != 0)
981 {
982 int len = vsnprintf(buf, INLINE_FORMAT_BUFFER_LEN, format_string, argList);
983 assign(buf, buf + len);
984 delete[] buf;
985 }
986 #endif
987 va_end(argList);
988 return *this;
989 }
990
991 /*******************************************************************************
992 Function: hl_lib::string_ref::trim_left
993 Access: public
994 Qualifier:
995 Parameter: value_type ch
996 Returns: string_ref&
997
998 Purpose: delete all char which is ch at the left of the string
999 *******************************************************************************/
1000 string_ref& trim_left(value_type ch = ' ')
1001 {
1002 size_type off = this->find_first_not_of(ch);
1003 if (off != string::npos)
1004 {
1005 this->erase(0, off);
1006 }
1007 return *this;
1008 }
1009
1010 /*******************************************************************************
1011 Function: hl_lib::string_ref::trim_right
1012 Access: public
1013 Qualifier:
1014 Parameter: value_type ch
1015 Returns: string_ref&
1016
1017 Purpose: delete all char which is ch at the right of the string
1018 *******************************************************************************/
1019 string_ref& trim_right(value_type ch = ' ')
1020 {
1021 size_type off = this->find_last_not_of(ch);
1022 if (off == string::npos)
1023 {
1024 off = 0;
1025 }
1026 else
1027 {
1028 off++;
1029 }
1030 this->erase(off, length() - off);
1031 return *this;
1032 }
1033
1034 /*******************************************************************************
1035 Function: hl_lib::string_ref::trim
1036 Access: public
1037 Qualifier:
1038 Parameter: value_type ch
1039 Returns: string_ref&
1040
1041 Purpose: delete all char which is ch at the left and right of the string
1042 *******************************************************************************/
1043 string_ref& trim(value_type ch = ' ')
1044 {
1045 trim_left(ch);
1046 trim_right(ch);
1047 return *this;
1048 }
1049
1050 /*******************************************************************************
1051 Function: hl_lib::string_ref::float_num
1052 Access: public static
1053 Qualifier:
1054 Parameter: const string & str
1055 Returns: double
1056
1057 Purpose: parse str to a float number
1058 *******************************************************************************/
1059 static double float_num(string const& str)
1060 {
1061 return atof(str.c_str());
1062 }
1063
1064 /*******************************************************************************
1065 Function: hl_lib::string_ref::float_num
1066 Access: public static
1067 Qualifier:
1068 Returns: double
1069
1070 Purpose: parse this to a float number
1071 *******************************************************************************/
1072 double float_num() const
1073 {
1074 return float_num(*this);
1075 }
1076
1077 /*******************************************************************************
1078 Function: hl_lib::string_ref::compare_nocase
1079 Access: public
1080 Qualifier: const
1081 Parameter: const string & str
1082 Returns: int
1083
1084 Purpose: compare string no case
1085 *******************************************************************************/
1086 int compare_nocase(string const& str) const
1087 {
1088 #ifdef _WIN32
1089 return _stricmp(this->c_str(), str.c_str());
1090 #else
1091 return strcasecmp(this->c_str(), str.c_str());
1092 #endif
1093 }
1094
1095 /*******************************************************************************
1096 Function: hl_lib::string_ref::compare_nocase
1097 Access: public
1098 Qualifier: const
1099 Parameter: size_type index
1100 Parameter: size_type length
1101 Parameter: const string & str
1102 Returns: int
1103
1104 Purpose: compare substring no case
1105 *******************************************************************************/
1106 int compare_nocase(size_type index, size_type length, string const& str) const
1107 {
1108 string_ref temp = this->substr(index, length);
1109 return temp.compare_nocase(str);
1110 }
1111
1112 /*******************************************************************************
1113 Function: hl_lib::string_ref::compare_nocase
1114 Access: public
1115 Qualifier: const
1116 Parameter: size_type index
1117 Parameter: size_type length
1118 Parameter: const string & str
1119 Parameter: size_type index2
1120 Parameter: size_type length2
1121 Returns: int
1122
1123 Purpose: compare two substring no case
1124 *******************************************************************************/
1125 int compare_nocase(size_type index, size_type length, string const& str, size_type index2, size_type length2) const
1126 {
1127 string_ref temp1 = this->substr(index, length);
1128 string_ref temp2 = str.substr(index2, length2);
1129 return temp1.compare_nocase(temp2);
1130 }
1131 };
1132
1133 } // namespace std