ã¿ããªå¤§å¥½ãstd::string_viewã¯ãä»»æã®æååããã¤ããªãã¤ãåã¸ã®ããã¥ã¼ãã表ãããã«ä½¿ããããDBMã©ã¤ãã©ãªTkrzwã§ã¯std::string_viewã®ééã£ã使ãæ¹ããã¦ããããã«ãææ°ã®å¦çç³»ã§ãã«ãããã¨å
¨ãåããªããªãã¨ããä¸å
·åããã£ãããã®å¯¾å¦ã«è¦å´ããã¨ãã話ã
std::string_viewã¨ã¯ãåç´åããã¨ãã¤ã³ã¿ã¨ãµã¤ãºã®æ§é ä½ã ã以ä¸ã®ããã«è¡¨ç¾ãã¦ãè¯ãã
struct string_view { const char* data; size_t size; };
dataãæã示ãè¨æ¶é åã¸ã®ææ権ãæããªãã®ãstd::stringã¨ã®æç½ãªéãã ãstd::string_viewã¯åãªããã¥ã¼ã§ãããçæã³ã¹ããé常ã«å°ãããé¢æ°ã®ãã©ã¡ã¼ã¿ã¨ãã¦å®ä½æ¸¡ãããéã®ã³ã¹ããå°ããããã£ã¦ãå®è¡é度ãæ°ã«ããããã°ã©ã ã§ã¯std::stringã§ã¯ãªãstd::string_viewãã§ããéãå¤ç¨ããã
å½ç¶ãªããTkrzwã§ãstd::string_viewã使ãã¾ãã£ã¦ããããã ãããã®ä¸ã§ç¹æ®ãªä½¿ç¨æ³ããã¦ããã¨ãããããããã¡ã¤ã«ä¸ã®ãã¼ã¿ãããããã¡ã¢ãªä¸ã«ããããã«è¦ããããããã«ããã¡ã¤ã«ä¸ã®ãªãã»ããã¨std::string_viewã®ãã¢ãä¿æããæ§é ä½ã使ã£ã¦ãããåç´åããã¨ãããªæãã ã
struct Record { off_t off_; std::string_view data_; std::string_view Get() const; };
ã¬ã³ã¼ãã«ã¢ã¯ã»ã¹ããé¢æ°ãå¼ã¶ã¨ã該å½ã®é åãã¡ã¢ãªã«èªã¿è¾¼ã¾ãã¦ããå ´åã«ã¯ãstd::string_viewã«ãã¤ã³ã¿ã¨ãµã¤ãºãä¸ãã¦ä½ã£ã¦è¿ãã該å½ã®é åãã¡ã¢ãªã«èªã¿è¾¼ã¾ãã¦ããªãå ´åã«ã¯ãstd::string_view(nullptr, size) ã¨ãã¦ãnullptrã¨ãµã¤ãºã ããä¸ãã¦ä½ã£ã¦è¿ããå®éã«ãã¼ã¿ãåç §ããéã«é 延èªã¿è¾¼ã¿ãè¡ãããã®å¾è ã®nullptrã§std::string_viewãæ§ç¯ããç¨æ³ãä»æ§éåãªã®ã ãCPlusPlus Referenceãããã®ã³ã³ã¹ãã©ã¯ã¿ã®ä»æ§ãæç²ããã
- constexpr basic_string_view( const CharT* s, size_type count );
- Constructs a view of the first count characters of the character array starting with the element pointed by s. s can contain null characters. The behavior is undefined if [s, s + count) is not a valid range (even though the constructor may not access any of the elements of this range). After construction, data() is equal to s, and size() is equal to count.
ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã§ã¯nullptrã¨0ã®ãã¢ã¨ãã¦ãªãã¸ã§ã¯ããä½ãããã®ã§ããã©ã¡ã¼ã¿ä»ãã®ã³ã³ã¹ãã©ã¯ã¿ã§ãnullptrãä¸ããããã¨ç§ã¯åéããã¦ãããstd::string_viewã¯ãã¥ã¼ãªã®ã ãããçæããæç¹ã§ã¯ãã¤ã³ã¿ã®å ãä½ã§ããããæç¥ããªãã¨æã£ã¦ããã®ã ãããã¦å¤ãã®å¦çç³»ã§ã¯å®éã«nullptrãä¸ãã¦ãæ®éã«åãã¦ãããããããä»æ§ã¨ãã¦ã¯ãã¤ã³ã¿ã®å ã®é åã妥å½ãªã¡ã¢ãªé åã§ãããã¨ãè¦æ±ãã¦ãããã©ããªç®çã§ãã®ãããªå¶ç´ãè¨ãã¦ããã®ãã¯ããããªãããã¨ã«ããä»æ§ä¸ã¯ãããªã®ã ããã®ããããæè¿ã®ä¸é¨ã®å¦çç³»ã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ã®ä¸ã«ãã©ã¡ã¼ã¿ãnullptrã§ãªããã¨ã調ã¹ãassertãåãè¾¼ãã§ãããã¨ãããããã®å ´åãnullptrãä¸ããã°å½ç¶ããã°ã©ã ã¯abortãã¦ãã¾ãããã¬ã¬ãèªåã®ç°å¢ã§ãã¹ãã«éãã ãããã ãã§ãä¾åããAPIã®å ¨ã¦ã®ä»æ§ã¯ã¡ããã¨èªã¾ãªãã¨ãããªãã
å°æå ã®å¯¾çã¨ãã¦ã¯ãnullptrã®ä»£ããã«reinterpret_cast< const char*>(1)ã¨ãã使ãæãããã1ã¯nullptrãããªãã®ã§assert(data != nullptr) ã¿ãããªå ¸åçãªæ¤æ»ã«ã¯å¼ã£ããããªããã1ãæ£å¸¸ãªãã¤ã³ã¿ã¨ãã¦ä½¿ãå¦çç³»ããªãã®ã§åºå¥ã§ãããããããæ£å¸¸ãªãã¤ã³ã¿ãããªããã®ãä¸ããã®ã¯ä¾ç¶ã¨ãã¦ä»æ§éåã ãä½ããã®æ¹æ³ã§åç §å ã®å¦¥å½æ§æ¤è¨¼ãããå¦çç³»ãããã°ã¯ã©ãã·ã¥ãã¦ãã¾ãã ããã
ä»æ¹ãªãã®ã§ãnullptrãæ±ããããããªãå ´æã§ã¯std::string_viewã使ãã®ãæ¢ãã¦ãã»ã¼åãæ©è½ã®tkrzw::NullableStringViewã¨ããã¯ã©ã¹ãæ¸ãã¦ä»£ç¨ãããä¼ç¤¾ã§ãããªè»è¼ªã®åçºæããããã³ã¼ãã¬ãã¥ã¼ã§ãã£ã¡ãè©°åããããã ããã©ãããããããªããç§ã«è¨ãããã°ãstd::string_viewã®ã³ã³ã¹ãã©ã¯ã¿ã«å¤ãªå¶ç´ãã¤ãã¦ããæ¹ãããããããã¥ã¼ã«éããªããã ããåæã«åç §å ã観測ãããªãã¨è¨ããããã©ãã§ããããã¨ã ãã観測ããã ãã§çµæãå¤ãã£ã¦ãã¾ãã£ã¦ã®ã¯éååå¦ã£ã½ãã¦ã¡ãã£ã¨æµªæ¼«ãæããã
ãã¨ã¯ãå¿ è¦ãªå ´æã§ãã®tkrzw::NullableStringViewã使ãããã«æ¢åã®ã³ã¼ããæ¸ãæãã¦ããã°ãããã¨ã«ãªããããããã©ããæ¸ãæããã¹ããã«å®å ¨ãªèªä¿¡ãæã¤ã®ã¯é£ãããããã§ãã¾ãã¯å ±åãããåé¡ãèªåã®ç°å¢ã§ãåç¾ããããã«ããããã¨ãã£ã¦ãå¦çç³»ãå ¥ãæ¿ããã®ã¯æéãªã®ã§ãã¡ãã£ã¨ããããã¯ã§ä¹ãåããã¨ã«ãããstring_viewã®ã³ã³ã¹ãã©ã¯ã¿ã«nullptrã渡ãã¨è½ã¡ãã¨ããæåãä»»æã®å¦çç³»ã§å¼·å¶ããã«ã¯ãæ¨æºã©ã¤ãã©ãªã®ããããæ¸ãæãã¦ãã¾ãã°è¯ãã/usr/include/c++/11/string_view ãã¨ãã£ã¿ã§éãã¦ãã³ã³ã¹ãã©ã¯ã¿ã«assertã¢ããã追å ããã
constexpr basic_string_view(const _CharT* __str, size_type __len) noexcept : _M_len{__len}, _M_str{__str} { // Assures that the given pointer is not nullptr. if (__str == nullptr) { abort(); } }
ãã®ç¶æ ã§ãã«ããç´ãã°ãå ±åãããåé¡ãåç¾ããããã«ãªã£ãããã¨ã¯ããã¹ãã±ã¼ã¹ãåããã¦ã¯ã©ãã·ã¥ããã¦ã¹ã¿ãã¯ãã¬ã¼ã¹ãè¦ãªãããè½ã¡ãå ´æã®std::string_viewãtkrzw::NullableStringViewã«ç½®ãæãã¦ããã°ããããã¹ãã±ã¼ã¹ã®ã«ãã¬ãã¸ãååã§ããã°ãããã ãã§åé¡ã解決ã§ããããã¡ããé¢é£ããã³ã¼ããèªã¿ç´ãã¦æå³çãªåä½ã§ãããã®ç¢ºèªãããã
ã¨ããçµç·¯ã§ãæ½å¨çã«ã©ã®å¦çç³»ã§ãåºæ¬æ©è½ã§ã¯ã©ãã·ã¥ããããªãã¨ããé大ãã°ã解決ããTkrzw 1.0.32ããªãªã¼ã¹ãããä¹ ãã¶ãã«è¶£å³ã®ã³ã¼ããæ¸ãã¨æ¥½ãããã®ã ã