tuklib_mbstr.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file tuklib_mstr.h
  4. /// \brief Utility functions for handling multibyte strings
  5. ///
  6. /// If not enough multibyte string support is available in the C library,
  7. /// these functions keep working with the assumption that all strings
  8. /// are in a single-byte character set without combining characters, e.g.
  9. /// US-ASCII or ISO-8859-*.
  10. //
  11. // Author: Lasse Collin
  12. //
  13. // This file has been put into the public domain.
  14. // You can do whatever you want with this file.
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #ifndef TUKLIB_MBSTR_H
  18. #define TUKLIB_MBSTR_H
  19. #include "tuklib_common.h"
  20. TUKLIB_DECLS_BEGIN
  21. #define tuklib_mbstr_width TUKLIB_SYMBOL(tuklib_mbstr_width)
  22. extern size_t tuklib_mbstr_width(const char *str, size_t *bytes);
  23. ///<
  24. /// \brief Get the number of columns needed for the multibyte string
  25. ///
  26. /// This is somewhat similar to wcswidth() but works on multibyte strings.
  27. ///
  28. /// \param str String whose width is to be calculated. If the
  29. /// current locale uses a multibyte character set
  30. /// that has shift states, the string must begin
  31. /// and end in the initial shift state.
  32. /// \param bytes If this is not NULL, *bytes is set to the
  33. /// value returned by strlen(str) (even if an
  34. /// error occurs when calculating the width).
  35. ///
  36. /// \return On success, the number of columns needed to display the
  37. /// string e.g. in a terminal emulator is returned. On error,
  38. /// (size_t)-1 is returned. Possible errors include invalid,
  39. /// partial, or non-printable multibyte character in str, or
  40. /// that str doesn't end in the initial shift state.
  41. #define tuklib_mbstr_fw TUKLIB_SYMBOL(tuklib_mbstr_fw)
  42. extern int tuklib_mbstr_fw(const char *str, int columns_min);
  43. ///<
  44. /// \brief Get the field width for printf() e.g. to align table columns
  45. ///
  46. /// Printing simple tables to a terminal can be done using the field field
  47. /// feature in the printf() format string, but it works only with single-byte
  48. /// character sets. To do the same with multibyte strings, tuklib_mbstr_fw()
  49. /// can be used to calculate appropriate field width.
  50. ///
  51. /// The behavior of this function is undefined, if
  52. /// - str is NULL or not terminated with '\0';
  53. /// - columns_min <= 0; or
  54. /// - the calculated field width exceeds INT_MAX.
  55. ///
  56. /// \return If tuklib_mbstr_width(str, NULL) fails, -1 is returned.
  57. /// If str needs more columns than columns_min, zero is returned.
  58. /// Otherwise a positive integer is returned, which can be
  59. /// used as the field width, e.g. printf("%*s", fw, str).
  60. TUKLIB_DECLS_END
  61. #endif