You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/text/benefits-of-character-set-portability.md
+11-9Lines changed: 11 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,13 +12,15 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# Benefits of Character Set Portability
15
-
You can benefit from using MFC and C run-time portability features even if you do not currently intend to internationalize your application:
16
-
17
-
- Coding portably makes your code base flexible. You can later move it easily to Unicode or MBCS.
18
-
19
-
- Using Unicode makes your applications for Windows more efficient. Because Windows uses Unicode, non-Unicode strings passed to and from the operating system must be translated, which incurs overhead.
20
15
21
-
22
-
## See Also
23
-
[Unicode and MBCS](../text/unicode-and-mbcs.md)
24
-
[Support for Unicode](../text/support-for-unicode.md)
16
+
You can benefit from using MFC and C run-time portability features even if you do not currently intend to internationalize your application:
17
+
18
+
- Coding portably makes your code base flexible. You can later move it easily to Unicode or MBCS.
19
+
20
+
- Using Unicode makes your applications for Windows more efficient. Because Windows uses Unicode, non-Unicode strings passed to and from the operating system must be translated, which incurs overhead.
21
+
22
+
23
+
## See Also
24
+
25
+
[Unicode and MBCS](../text/unicode-and-mbcs.md)<br/>
26
+
[Support for Unicode](../text/support-for-unicode.md)
Copy file name to clipboardExpand all lines: docs/text/buffer-overflow.md
+42-40Lines changed: 42 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,43 +12,45 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# Buffer Overflow
15
-
Varying character sizes can cause problems when you put characters into a buffer. Consider the following code, which copies characters from a string, `sz`, into a buffer, `rgch`:
16
-
17
-
```
18
-
cb = 0;
19
-
while( cb < sizeof( rgch ) )
20
-
rgch[ cb++ ] = *sz++;
21
-
```
22
-
23
-
The question is: Was the last byte copied a lead byte? The following does not solve the problem because it can potentially overflow the buffer:
24
-
25
-
```
26
-
cb = 0;
27
-
while( cb < sizeof( rgch ) )
28
-
{
29
-
_mbccpy( rgch + cb, sz );
30
-
cb += _mbclen( sz );
31
-
sz = _mbsinc( sz );
32
-
}
33
-
```
34
-
35
-
The `_mbccpy` call attempts to do the correct thing — copy the full character, whether it is 1 or 2 bytes. But it does not take into account that the last character copied might not fit the buffer if the character is 2 bytes wide. The correct solution is:
36
-
37
-
```
38
-
cb = 0;
39
-
while( (cb + _mbclen( sz )) <= sizeof( rgch ) )
40
-
{
41
-
_mbccpy( rgch + cb, sz );
42
-
cb += _mbclen( sz );
43
-
sz = _mbsinc( sz );
44
-
}
45
-
```
46
-
47
-
This code tests for possible buffer overflow in the loop test, using `_mbclen` to test the size of the current character pointed to by `sz`. By making a call to the `_mbsnbcpy` function, you can replace the code in the **while** loop with a single line of code. For example:
Varying character sizes can cause problems when you put characters into a buffer. Consider the following code, which copies characters from a string, `sz`, into a buffer, `rgch`:
17
+
18
+
```cpp
19
+
cb = 0;
20
+
while( cb < sizeof( rgch ) )
21
+
rgch[ cb++ ] = *sz++;
22
+
```
23
+
24
+
The question is: Was the last byte copied a lead byte? The following does not solve the problem because it can potentially overflow the buffer:
25
+
26
+
```cpp
27
+
cb = 0;
28
+
while( cb < sizeof( rgch ) )
29
+
{
30
+
_mbccpy( rgch + cb, sz );
31
+
cb += _mbclen( sz );
32
+
sz = _mbsinc( sz );
33
+
}
34
+
```
35
+
36
+
The `_mbccpy` call attempts to do the correct thing — copy the full character, whether it is 1 or 2 bytes. But it does not take into account that the last character copied might not fit the buffer if the character is 2 bytes wide. The correct solution is:
37
+
38
+
```cpp
39
+
cb = 0;
40
+
while( (cb + _mbclen( sz )) <= sizeof( rgch ) )
41
+
{
42
+
_mbccpy( rgch + cb, sz );
43
+
cb += _mbclen( sz );
44
+
sz = _mbsinc( sz );
45
+
}
46
+
```
47
+
48
+
This code tests for possible buffer overflow in the loop test, using `_mbclen` to test the size of the current character pointed to by `sz`. By making a call to the `_mbsnbcpy` function, you can replace the code in the **while** loop with a single line of code. For example:
Copy file name to clipboardExpand all lines: docs/text/byte-indices.md
+25-23Lines changed: 25 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,26 +12,28 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# Byte Indices
15
-
Use the following tips:
16
-
17
-
- Working with a bytewise index into a string presents problems similar to those posed by pointer manipulation. Consider this example, which scans a string for a backslash character:
18
-
19
-
```
20
-
while ( rgch[ i ] != '\\' )
21
-
i++;
22
-
```
23
-
24
-
This might index a trail byte, not a lead byte, and thus it might not point to a `character`.
25
-
26
-
- Use the [_mbclen](../c-runtime-library/reference/mbclen-mblen-mblen-l.md) function to solve the preceding problem:
27
-
28
-
```
29
-
while ( rgch[ i ] != '\\' )
30
-
i += _mbclen ( rgch + i );
31
-
```
32
-
33
-
This correctly indexes to a lead byte, hence to a `character`. The `_mbclen` function determines the size of a character (1 or 2 bytes).
[Last Character in a String](../text/last-character-in-a-string.md)
15
+
16
+
Use the following tips:
17
+
18
+
- Working with a bytewise index into a string presents problems similar to those posed by pointer manipulation. Consider this example, which scans a string for a backslash character:
19
+
20
+
```cpp
21
+
while ( rgch[ i ] != '\\' )
22
+
i++;
23
+
```
24
+
25
+
This might index a trail byte, not a lead byte, and thus it might not point to a `character`.
26
+
27
+
- Use the [_mbclen](../c-runtime-library/reference/mbclen-mblen-mblen-l.md) function to solve the preceding problem:
28
+
29
+
```cpp
30
+
while ( rgch[ i ] != '\\' )
31
+
i += _mbclen ( rgch + i );
32
+
```
33
+
34
+
This correctly indexes to a lead byte, hence to a `character`. The `_mbclen` function determines the size of a character (1 or 2 bytes).
Copy file name to clipboardExpand all lines: docs/text/character-assignment.md
+33-31Lines changed: 33 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,34 +12,36 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# Character Assignment
15
-
Consider the following example, in which the **while** loop scans a string, copying all characters except 'X' into another string:
16
-
17
-
```
18
-
while( *sz2 )
19
-
{
20
-
if( *sz2 != 'X' )
21
-
*sz1++ = *sz2++;
22
-
else
23
-
sz2++;
24
-
}
25
-
```
26
-
27
-
The code copies the byte at `sz2` to the location pointed to by `sz1`, then increments `sz1` to receive the next byte. But if the next character in `sz2` is a double-byte character, the assignment to `sz1` copies only the first byte. The following code uses a portable function to copy the character safely and another to increment `sz1` and `sz2` correctly:
Consider the following example, in which the **while** loop scans a string, copying all characters except 'X' into another string:
17
+
18
+
```cpp
19
+
while( *sz2 )
20
+
{
21
+
if( *sz2 != 'X' )
22
+
*sz1++ = *sz2++;
23
+
else
24
+
sz2++;
25
+
}
26
+
```
27
+
28
+
The code copies the byte at `sz2` to the location pointed to by `sz1`, then increments `sz1` to receive the next byte. But if the next character in `sz2` is a double-byte character, the assignment to `sz1` copies only the first byte. The following code uses a portable function to copy the character safely and another to increment `sz1` and `sz2` correctly:
Copy file name to clipboardExpand all lines: docs/text/general-mbcs-programming-advice.md
+23-21Lines changed: 23 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,24 +13,26 @@ ms.author: "mblome"
13
13
ms.workload: ["cplusplus"]
14
14
---
15
15
# General MBCS Programming Advice
16
-
Use the following tips:
17
-
18
-
- For flexibility, use run-time macros such as `_tcschr` and `_tcscpy` when possible. For more information, see [Generic-Text Mappings in Tchar.h](../text/generic-text-mappings-in-tchar-h.md).
19
-
20
-
- Use the C run-time `_getmbcp` function to get information about the current code page.
21
-
22
-
- Do not reuse string resources. Depending on the target language, a given string might have a different meaning when translated. For example, "File" on the application's main menu might translate differently from the string "File" in a dialog box. If you need to use more than one string with the same name, use different string IDs for each.
23
-
24
-
- You might want to find out whether your application is running on an MBCS-enabled operating system. To do so, set a flag at program startup; do not rely on API calls.
25
-
26
-
- When designing dialog boxes, allow approximately 30% extra space at the end of static text controls for MBCS translation.
27
-
28
-
- Be careful when selecting fonts for your application, because some fonts are not available on all systems.
29
-
30
-
- When selecting the font for dialog boxes, use [MS Shell Dlg](/windows/desktop/Intl/using-ms-shell-dlg-and-ms-shell-dlg-2) instead of MS Sans Serif or Helvetica. MS Shell Dlg is replaced with the correct font by the system before creating the dialog box. Using MS Shell Dlg ensures that any changes in the operating system to deal with this font will automatically be available. (MFC replaces MS Shell Dlg with the DEFAULT_GUI_FONT or the System font on Windows 95, Windows 98, and Windows NT 4 because those systems do not handle MS Shell Dlg correctly.)
31
-
32
-
- When designing your application, decide which strings can be localized. If in doubt, assume that any given string will be localized. As such, do not mix strings that can be localized with those that cannot.
[Incrementing and Decrementing Pointers](../text/incrementing-and-decrementing-pointers.md)
16
+
17
+
Use the following tips:
18
+
19
+
- For flexibility, use run-time macros such as `_tcschr` and `_tcscpy` when possible. For more information, see [Generic-Text Mappings in Tchar.h](../text/generic-text-mappings-in-tchar-h.md).
20
+
21
+
- Use the C run-time `_getmbcp` function to get information about the current code page.
22
+
23
+
- Do not reuse string resources. Depending on the target language, a given string might have a different meaning when translated. For example, "File" on the application's main menu might translate differently from the string "File" in a dialog box. If you need to use more than one string with the same name, use different string IDs for each.
24
+
25
+
- You might want to find out whether your application is running on an MBCS-enabled operating system. To do so, set a flag at program startup; do not rely on API calls.
26
+
27
+
- When designing dialog boxes, allow approximately 30% extra space at the end of static text controls for MBCS translation.
28
+
29
+
- Be careful when selecting fonts for your application, because some fonts are not available on all systems.
30
+
31
+
- When selecting the font for dialog boxes, use [MS Shell Dlg](/windows/desktop/Intl/using-ms-shell-dlg-and-ms-shell-dlg-2) instead of MS Sans Serif or Helvetica. MS Shell Dlg is replaced with the correct font by the system before creating the dialog box. Using MS Shell Dlg ensures that any changes in the operating system to deal with this font will automatically be available. (MFC replaces MS Shell Dlg with the DEFAULT_GUI_FONT or the System font on Windows 95, Windows 98, and Windows NT 4 because those systems do not handle MS Shell Dlg correctly.)
32
+
33
+
- When designing your application, decide which strings can be localized. If in doubt, assume that any given string will be localized. As such, do not mix strings that can be localized with those that cannot.
0 commit comments