pw_string: Match std::string semantics for literal / array overloads

Previously, the constructor, assign, and append overloads for string
literals or arrays used the length of the array as the length of the
string, and dropped the final character only if it was a null
terminator.

This avoided some strlen() calls and made assigning to an InlineString
more like working with an ""sv string literal. However, since the
character array is not guaranteed to be null terminated, this approach
had some drawbacks:

- A string of length N could not be assigned to an InlineString<N>, to
  account for non-terminated character arrays.
- The capacity template argument would be deduced to a value one larger
  than the length of the string.
- Behavior was inconsistent with std::string, since nulls could be
  included in assignments.

This commit updates pw::InlineString's string literal or array overloads
to use the same semantics as std::string. Unlike std::string,
pw::InlineString checks the array size to prevent out-of-bounds reads.

Change-Id: Ic0e61fd1c45847f5a0b6affa414ed0e624b09807
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/111711
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Erik Gilling <konkers@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
5 files changed
tree: 69411123e996a637c6308240593d2b324c1014e9
  1. .allstar/
  2. .vscode/
  3. build_overrides/
  4. docker/
  5. docs/
  6. pw_allocator/
  7. pw_analog/
  8. pw_android_toolchain/
  9. pw_arduino_build/
  10. pw_assert/
  11. pw_assert_basic/
  12. pw_assert_log/
  13. pw_assert_tokenized/
  14. pw_assert_zephyr/
  15. pw_base64/
  16. pw_bloat/
  17. pw_blob_store/
  18. pw_bluetooth/
  19. pw_bluetooth_hci/
  20. pw_boot/
  21. pw_boot_cortex_m/
  22. pw_build/
  23. pw_build_info/
  24. pw_build_mcuxpresso/
  25. pw_bytes/
  26. pw_checksum/
  27. pw_chrono/
  28. pw_chrono_embos/
  29. pw_chrono_freertos/
  30. pw_chrono_stl/
  31. pw_chrono_threadx/
  32. pw_chrono_zephyr/
  33. pw_cli/
  34. pw_compilation_testing/
  35. pw_console/
  36. pw_containers/
  37. pw_cpu_exception/
  38. pw_cpu_exception_cortex_m/
  39. pw_crypto/
  40. pw_digital_io/
  41. pw_docgen/
  42. pw_doctor/
  43. pw_env_setup/
  44. pw_file/
  45. pw_function/
  46. pw_fuzzer/
  47. pw_hdlc/
  48. pw_hex_dump/
  49. pw_i2c/
  50. pw_i2c_mcuxpresso/
  51. pw_ide/
  52. pw_interrupt/
  53. pw_interrupt_cortex_m/
  54. pw_interrupt_zephyr/
  55. pw_kvs/
  56. pw_libc/
  57. pw_log/
  58. pw_log_android/
  59. pw_log_basic/
  60. pw_log_null/
  61. pw_log_rpc/
  62. pw_log_string/
  63. pw_log_tokenized/
  64. pw_log_zephyr/
  65. pw_malloc/
  66. pw_malloc_freelist/
  67. pw_metric/
  68. pw_minimal_cpp_stdlib/
  69. pw_module/
  70. pw_multisink/
  71. pw_package/
  72. pw_persistent_ram/
  73. pw_polyfill/
  74. pw_preprocessor/
  75. pw_presubmit/
  76. pw_protobuf/
  77. pw_protobuf_compiler/
  78. pw_random/
  79. pw_result/
  80. pw_ring_buffer/
  81. pw_router/
  82. pw_rpc/
  83. pw_rust/
  84. pw_snapshot/
  85. pw_software_update/
  86. pw_span/
  87. pw_spi/
  88. pw_status/
  89. pw_stm32cube_build/
  90. pw_stream/
  91. pw_string/
  92. pw_symbolizer/
  93. pw_sync/
  94. pw_sync_baremetal/
  95. pw_sync_embos/
  96. pw_sync_freertos/
  97. pw_sync_stl/
  98. pw_sync_threadx/
  99. pw_sync_zephyr/
  100. pw_sys_io/
  101. pw_sys_io_arduino/
  102. pw_sys_io_baremetal_lm3s6965evb/
  103. pw_sys_io_baremetal_stm32f429/
  104. pw_sys_io_emcraft_sf2/
  105. pw_sys_io_mcuxpresso/
  106. pw_sys_io_stdio/
  107. pw_sys_io_stm32cube/
  108. pw_sys_io_zephyr/
  109. pw_system/
  110. pw_target_runner/
  111. pw_thread/
  112. pw_thread_embos/
  113. pw_thread_freertos/
  114. pw_thread_stl/
  115. pw_thread_threadx/
  116. pw_tls_client/
  117. pw_tls_client_boringssl/
  118. pw_tls_client_mbedtls/
  119. pw_tokenizer/
  120. pw_tool/
  121. pw_toolchain/
  122. pw_trace/
  123. pw_trace_tokenized/
  124. pw_transfer/
  125. pw_unit_test/
  126. pw_varint/
  127. pw_watch/
  128. pw_web/
  129. pw_work_queue/
  130. targets/
  131. third_party/
  132. ts/
  133. zephyr/
  134. .bazelignore
  135. .bazelrc
  136. .clang-format
  137. .clang-tidy
  138. .eslintrc.json
  139. .gitattributes
  140. .gitignore
  141. .gn
  142. .prettierrc.js
  143. .pw_ide.yaml
  144. .pylintrc
  145. activate.bat
  146. Android.bp
  147. AUTHORS
  148. bootstrap.bat
  149. bootstrap.sh
  150. BUILD.bazel
  151. BUILD.gn
  152. BUILDCONFIG.gn
  153. CMakeLists.txt
  154. jest.config.ts
  155. Kconfig.zephyr
  156. LICENSE
  157. modules.gni
  158. OWNERS
  159. package-lock.json
  160. package.json
  161. PIGWEED_MODULES
  162. PW_PLUGINS
  163. README.md
  164. rollup.config.js
  165. tsconfig.json
  166. WORKSPACE
README.md

Pigweed

Pigweed is an open source collection of embedded-targeted libraries–or as we like to call them, modules. These modules are building blocks and infrastructure that enable faster and more reliable development on small-footprint MMU-less 32-bit microcontrollers like the STMicroelectronics STM32L452 or the Nordic nRF52832.

For more information please see our website: https://pigweed.dev/.

Links