MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / strDup

Method strDup

source/SimpleHeap.cpp:27–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25UINT SimpleHeap::sBlockCount = 0;
26
27LPTSTR SimpleHeap::strDup(LPCTSTR aBuf, size_t aLength)
28// v1.0.44.14: Added aLength to improve performance in cases where callers already know the length.
29// If aLength is at its default of -1, the length will be calculated here.
30// Caller must ensure that aBuf isn't NULL.
31{
32 if (!aBuf || !*aBuf) // aBuf is checked for NULL because it's not worth avoiding it for such a low-level, frequently-called function.
33 return _T(""); // Return the constant empty string to the caller (not aBuf itself since that might be volatile).
34 if (aLength == -1) // Caller wanted us to calculate it. Compare directly to -1 since aLength is unsigned.
35 aLength = _tcslen(aBuf);
36 LPTSTR new_buf;
37 if ( !(new_buf = (LPTSTR)SimpleHeap::Malloc((aLength + 1) * sizeof(TCHAR))) ) // +1 for the zero terminator.
38 return NULL; // Callers may rely on NULL vs. "" being returned in the event of failure.
39 if (aLength)
40 tmemcpy(new_buf, aBuf, aLength); // memcpy() typically benchmarks slightly faster than strcpy().
41 //else only a terminator is needed.
42 new_buf[aLength] = '\0'; // Terminate here for when aLength==0 and for the memcpy above so that caller's aBuf doesn't have to be terminated.
43 return new_buf;
44}
45
46LPTSTR SimpleHeap::Malloc(LPCTSTR aBuf, size_t aLength)
47{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected