Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CNativeW クラスの SetString メソッドに std::wstring_view 型を受け取るものを追加 #1734

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

beru
Copy link
Contributor

@beru beru commented Oct 2, 2021

PR の目的

SetString メソッドを呼ぶ際に、std::wstring 型のインスタンスの c_str() メソッドでポインタを引数にして呼びだすと、CNativeW::SetString( const wchar_t* pszData ) の実装で std::wstring_view のコンストラクタで文字列の要素数を調べる処理が走ります。文字列の要素数の情報は std::wstring 型の size や length メソッドで定数時間で取得できるので、それを使う方が効率が良いと思い変更しました。

カテゴリ

  • リファクタリング

PR の背景

PR のメリット

.c_str() の記述が無くせるので記述が短くなります。

PR のデメリット (トレードオフとかあれば)

特にないと思います。

仕様・動作説明

PR の影響範囲

テスト内容

テスト1

手順

関連 issue, PR

参考資料

https://cpprefjp.github.io/reference/string/basic_string/length.html
https://cpprefjp.github.io/reference/string_view/basic_string_view.html

@beru beru added the refactoring リファクタリング 【ChangeLog除外】 label Oct 2, 2021
@AppVeyorBot
Copy link

Build sakura 1.0.3933 completed (commit 07e122c14d by @beru)

@beru beru changed the title CNativeW クラスの SetString メソッドに std::wstring 型を受け取るものを追加 CNativeW クラスの SetString メソッドに std::wstring_view 型を受け取るものを追加 Oct 3, 2021
@sonarcloud
Copy link

sonarcloud bot commented Oct 3, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

25.0% 25.0% Coverage
0.0% 0.0% Duplication

@AppVeyorBot
Copy link

Build sakura 1.0.3937 completed (commit 0d59e8cf3f by @beru)

@berryzplus
Copy link
Contributor

Draftのままですが、残課題とかありましたっけ?

@beru
Copy link
Contributor Author

beru commented Oct 9, 2021

Draftのままですが、残課題とかありましたっけ?

テスト未実施なのでDraftにしています。

@@ -78,6 +78,7 @@ class CNativeW final : public CNative{
//WCHAR
void SetString( const wchar_t* pData, size_t nDataLen ); //!< バッファの内容を置き換える。nDataLenは文字単位。
void SetString( const wchar_t* pszData ); //!< バッファの内容を置き換える。
void SetString( std::wstring_view str ) { SetString(str.data(), str.length()); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str.empty()true の場合(≒ str.length() == 0 のとき)に str.data() をアクセスすると落ちたような気がしないでもないです。

@berryzplus
Copy link
Contributor

このPRって続行します?

  • 趣旨
    CNativeW::SetString(std::wstring_view str)を追加することにより、
    dist.SetString(str.c_str(), str.length()) のような length() 呼出を削減し「高速化」したい。

  • 停止理由
    単体テストを整備したいが時間がとれない。

個人的に懸念は「修正によって既存コードがクラッシュする事態にならないか?」だけです。

CNativeW dist;
const wchar_t* pNull = nullptr;
dist.SetString(pNull); //←これがstd::wstirng_view型引数と解釈された場合、クラッシュしてしまう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring リファクタリング 【ChangeLog除外】
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants