Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: core/include/fxcrt/fx_string.h

Issue 1114313006: Make constructors recognize char[n] vs. char* and avoid strlen calls. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Delegated constructors everywhere. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | core/src/fpdfdoc/doc_basic.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #ifndef _FX_STRING_H_ 7 #ifndef _FX_STRING_H_
8 #define _FX_STRING_H_ 8 #define _FX_STRING_H_
9 9
10 #include <stdint.h> // For intptr_t. 10 #include <stdint.h> // For intptr_t.
11 #include <algorithm> 11 #include <algorithm>
12 12
13 #include "fx_memory.h" 13 #include "fx_memory.h"
14 14
15 class CFX_BinaryBuf; 15 class CFX_BinaryBuf;
16 class CFX_ByteString; 16 class CFX_ByteString;
17 class CFX_WideString; 17 class CFX_WideString;
18 struct CFX_CharMap; 18 struct CFX_CharMap;
19 typedef int FX_STRSIZE; 19 typedef int FX_STRSIZE;
20 20
21 // Technique upon which we build code to allow the compiler to tell
22 // the difference between a pointer for which it doesn't know the size,
23 // and an array where it does know the size (typically a const char string
24 // literal), until std::enable_if becomes available. See also:
25 // http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
26 template <typename T, typename U> struct FX_SFINAE { typedef T nonesuch; };
27 template <typename T> struct FX_SFINAE<T, T> { typedef T type; };
28
21 // An immutable string with caller-provided storage which must outlive the 29 // An immutable string with caller-provided storage which must outlive the
22 // string itself. 30 // string itself.
23 class CFX_ByteStringC 31 class CFX_ByteStringC
24 { 32 {
25 public: 33 public:
26 typedef FX_CHAR value_type; 34 typedef FX_CHAR value_type;
27 35
28 CFX_ByteStringC() 36 CFX_ByteStringC()
29 { 37 {
30 m_Ptr = NULL; 38 m_Ptr = NULL;
31 m_Length = 0; 39 m_Length = 0;
32 } 40 }
33 41
34 CFX_ByteStringC(FX_LPCBYTE ptr, FX_STRSIZE size) 42 CFX_ByteStringC(FX_LPCBYTE ptr, FX_STRSIZE size)
35 { 43 {
36 m_Ptr = ptr; 44 m_Ptr = ptr;
37 m_Length = size; 45 m_Length = size;
38 } 46 }
39 47
40 CFX_ByteStringC(FX_LPCSTR ptr) 48 // For the case where the compiler can't deduce the length.
41 { 49 template<typename T, typename = typename FX_SFINAE<T, char>::type>
42 m_Ptr = (FX_LPCBYTE)ptr; 50 CFX_ByteStringC(const T* const& ptr)
43 m_Length = ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0; 51 : CFX_ByteStringC(ptr, ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0) { }
44 } 52
53 // For the case where the compiler can deduce the length of a literal.
54 template<size_t N>
55 CFX_ByteStringC(const char (&ptr)[N])
56 : CFX_ByteStringC(ptr, N - 1) { }
brucedawson 2015/12/15 00:45:10 I don't think we have any guarantee that this is a
57
58 // For the case where the compiler can deduce the length, but not a literal.
59 template<size_t N>
60 CFX_ByteStringC(char (&ptr)[N])
61 : CFX_ByteStringC(ptr, ptr ? (FX_STRSIZE)FXSYS_strlen(ptr) : 0) { }
45 62
46 // |ch| must be an lvalue that outlives the the CFX_ByteStringC. However, 63 // |ch| must be an lvalue that outlives the the CFX_ByteStringC. However,
47 // the use of char rvalues are not caught at compile time. They are 64 // the use of char rvalues are not caught at compile time. They are
48 // implicitly promoted to CFX_ByteString (see below) and then the 65 // implicitly promoted to CFX_ByteString (see below) and then the
49 // CFX_ByteStringC is constructed from the CFX_ByteString via the alternate 66 // CFX_ByteStringC is constructed from the CFX_ByteString via the alternate
50 // constructor below. The CFX_ByteString then typically goes out of scope 67 // constructor below. The CFX_ByteString then typically goes out of scope
51 // and |m_Ptr| may be left pointing to invalid memory. Beware. 68 // and |m_Ptr| may be left pointing to invalid memory. Beware.
52 // TODO(tsepez): Mark single-argument string constructors as explicit. 69 // TODO(tsepez): Mark single-argument string constructors as explicit.
53 CFX_ByteStringC(FX_CHAR& ch) 70 CFX_ByteStringC(FX_CHAR& ch)
54 { 71 {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 214
198 CFX_ByteString() 215 CFX_ByteString()
199 { 216 {
200 m_pData = NULL; 217 m_pData = NULL;
201 } 218 }
202 219
203 CFX_ByteString(const CFX_ByteString& str); 220 CFX_ByteString(const CFX_ByteString& str);
204 221
205 CFX_ByteString(char ch); 222 CFX_ByteString(char ch);
206 223
207 CFX_ByteString(FX_LPCSTR ptr) 224 // For the case where the compiler can't deduce the length.
225 template<typename T, typename = typename FX_SFINAE<T, char>::type>
226 CFX_ByteString(const T* const& ptr)
208 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) { } 227 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) { }
209 228
229 // For the case where the compiler can deduce the length of a literal.
230 template<size_t N>
231 CFX_ByteString(const char (&ptr)[N])
232 : CFX_ByteString(ptr, N - 1) { }
233
234 // For the case where the compiler can deduce the length, but not a literal.
235 template<size_t N>
236 CFX_ByteString(char (&ptr)[N])
237 : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) { }
238
239
210 CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len); 240 CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len);
211
212 CFX_ByteString(FX_LPCBYTE ptr, FX_STRSIZE len); 241 CFX_ByteString(FX_LPCBYTE ptr, FX_STRSIZE len);
213 242
214 CFX_ByteString(FX_BSTR bstrc); 243 CFX_ByteString(FX_BSTR bstrc);
215 CFX_ByteString(FX_BSTR bstrc1, FX_BSTR bstrc2); 244 CFX_ByteString(FX_BSTR bstrc1, FX_BSTR bstrc2);
216 245
217 ~CFX_ByteString(); 246 ~CFX_ByteString();
218 247
219 static CFX_ByteString FromUnicode(FX_LPCWSTR ptr, FX_STRSIZE len = -1) ; 248 static CFX_ByteString FromUnicode(FX_LPCWSTR ptr, FX_STRSIZE len = -1) ;
220 249
221 static CFX_ByteString FromUnicode(const CFX_WideString& str); 250 static CFX_ByteString FromUnicode(const CFX_WideString& str);
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 { 485 {
457 public: 486 public:
458 typedef FX_WCHAR value_type; 487 typedef FX_WCHAR value_type;
459 488
460 CFX_WideStringC() 489 CFX_WideStringC()
461 { 490 {
462 m_Ptr = NULL; 491 m_Ptr = NULL;
463 m_Length = 0; 492 m_Length = 0;
464 } 493 }
465 494
466 CFX_WideStringC(FX_LPCWSTR ptr)
467 {
468 m_Ptr = ptr;
469 m_Length = ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0;
470 }
471
472 CFX_WideStringC(FX_WCHAR& ch) 495 CFX_WideStringC(FX_WCHAR& ch)
473 { 496 {
474 m_Ptr = &ch; 497 m_Ptr = &ch;
475 m_Length = 1; 498 m_Length = 1;
476 } 499 }
477 500
501 // For the case where the compiler can't deduce the length.
502 template<typename T, typename = typename FX_SFINAE<T, wchar_t>::type>
503 CFX_WideStringC(const T* const& ptr)
504 : CFX_WideStringC(ptr, ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0) { }
505
506 // For the case where the compiler can deduce the length of a literal.
507 template<size_t N>
508 CFX_WideStringC(const wchar_t (&ptr)[N])
509 : CFX_WideStringC(ptr, N - 1) { }
510
511 // For the case where the compiler can deduce the length, but not a literal.
512 template<size_t N>
513 CFX_WideStringC(wchar_t (&ptr)[N])
514 : CFX_WideStringC(ptr, ptr ? (FX_STRSIZE)FXSYS_wcslen(ptr) : 0) { }
515
478 CFX_WideStringC(FX_LPCWSTR ptr, FX_STRSIZE len) 516 CFX_WideStringC(FX_LPCWSTR ptr, FX_STRSIZE len)
479 { 517 {
480 m_Ptr = ptr; 518 m_Ptr = ptr;
481 if (len == -1) { 519 if (len == -1) {
482 m_Length = (FX_STRSIZE)FXSYS_wcslen(ptr); 520 m_Length = (FX_STRSIZE)FXSYS_wcslen(ptr);
483 } else { 521 } else {
484 m_Length = len; 522 m_Length = len;
485 } 523 }
486 } 524 }
487 525
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 public: 660 public:
623 typedef FX_WCHAR value_type; 661 typedef FX_WCHAR value_type;
624 662
625 CFX_WideString() 663 CFX_WideString()
626 { 664 {
627 m_pData = NULL; 665 m_pData = NULL;
628 } 666 }
629 667
630 CFX_WideString(const CFX_WideString& str); 668 CFX_WideString(const CFX_WideString& str);
631 669
632 CFX_WideString(FX_LPCWSTR ptr) 670 // For the case where the compiler can't deduce the length.
671 template<typename T, typename = typename FX_SFINAE<T, wchar_t>::type>
672 CFX_WideString(const T* const& ptr)
673 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) { }
674
675 // For the case where the compiler can deduce the length of a literal.
676 template<size_t N>
677 CFX_WideString(const wchar_t (&ptr)[N])
678 : CFX_WideString(ptr, N - 1) { }
679
680 // For the case where the compiler can deduce the length, but not a literal.
681 template<size_t N>
682 CFX_WideString(wchar_t (&ptr)[N])
633 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) { } 683 : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) { }
634 684
635 CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len); 685 CFX_WideString(FX_LPCWSTR ptr, FX_STRSIZE len);
636 686
637 CFX_WideString(FX_WCHAR ch); 687 CFX_WideString(FX_WCHAR ch);
638 688
639 CFX_WideString(const CFX_WideStringC& str); 689 CFX_WideString(const CFX_WideStringC& str);
640 690
641 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); 691 CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2);
642 692
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len); 922 CFX_ByteString FX_UTF8Encode(FX_LPCWSTR pwsStr, FX_STRSIZE len);
873 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr) 923 inline CFX_ByteString FX_UTF8Encode(FX_WSTR wsStr)
874 { 924 {
875 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength()); 925 return FX_UTF8Encode(wsStr.GetPtr(), wsStr.GetLength());
876 } 926 }
877 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr) 927 inline CFX_ByteString FX_UTF8Encode(const CFX_WideString &wsStr)
878 { 928 {
879 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()); 929 return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength());
880 } 930 }
881 #endif 931 #endif
OLDNEW
« no previous file with comments | « no previous file | core/src/fpdfdoc/doc_basic.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698