xref: /MusicPlayer2/scintilla/lexlib/StringCopy.h (revision 8af74909132ed5e696cb05b6689ae4baf14c1c96)
1 // Scintilla source code edit control
2 /** @file StringCopy.h
3  ** Safe string copy function which always NUL terminates.
4  ** ELEMENTS macro for determining array sizes.
5  **/
6 // Copyright 2013 by Neil Hodgson <[email protected]>
7 // The License.txt file describes the conditions under which this software may be distributed.
8 
9 #ifndef STRINGCOPY_H
10 #define STRINGCOPY_H
11 
12 namespace Scintilla {
13 
14 // Safer version of string copy functions like strcpy, wcsncpy, etc.
15 // Instantiate over fixed length strings of both char and wchar_t.
16 // May truncate if source doesn't fit into dest with room for NUL.
17 
18 template <typename T, size_t count>
StringCopy(T (& dest)[count],const T * source)19 void StringCopy(T (&dest)[count], const T* source) {
20 	for (size_t i=0; i<count; i++) {
21 		dest[i] = source[i];
22 		if (!source[i])
23 			break;
24 	}
25 	dest[count-1] = 0;
26 }
27 
28 #define ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
29 
30 }
31 
32 #endif
33