Pebble Coding

ソフトウェアエンジニアによるIT技術、数学の備忘録

C++20でstd::vectorを範囲forを逆順にする

C++11でstd::vectorを範囲forできるようになりましたが、逆順にはできませんでした。
C++20のrangesにより逆順で範囲forできるようになりました。

#include <vector>
#include <ranges>

std::vector<int> v = {0, 1, 2, 3};
for (const auto& a: v | std::views::reverse) {
    printf("%d ", a);
}
3 2 1 0

swift5.7(Xcode14)で気になる機能

SE-0345 if let shorthand for shadowing an existing optional variable

swift-evolution/proposals/0345-if-let-shorthand.md at main · apple/swift-evolution · GitHub

同じ変数名でunwrapする際に簡略化して書けるようになりました。
これ助かる。

Xcode13未満

let longVariableName: Int? = 3
guard let longVariableName = longVariableName else {
    ...
}

Xcode14以上

let longVariableName: Int? = 3
guard let longVariableName else {
    ...
}

std::filesystemの便利さ

std::filesystem::pathは便利である。
文字コード変換機能が内蔵されており,macOS,windowsどちらも意図どおり動作する。
以下検証コードと実行結果である。 

    std::filesystem::path path1 = "あ";
    const unsigned char* a = (const unsigned char*)path1.string().c_str();
    printf("a: %02x %02x %02x %02x\n", *a, *(a+1), *(a+2), *(a+3));
    const wchar_t* b = path1.wstring().c_str();
    printf("b: %02x %02x %02x %02x\n", *b, *(b+1), *(b+2), *(b+3));
    
    std::filesystem::path path2 = L"あ";
    const unsigned char* c = (const unsigned char*)path2.string().c_str();
    
    printf("c: %02x %02x %02x %02x\n", *c, *(c+1), *(c+2), *(c+3));
    const wchar_t* d = path2.wstring().c_str();
    printf("d: %02x %02x %02x %02x\n", *d, *(d+1), *(d+2), *(d+3));
[Xcode15]
a: 00e3 0081 0082 0000
b: 3042 0000 0000 0000
c: 00e3 0081 0082 0000
d: 3042 0000 0000 0000
[Visual Studio 2022]
a: 0082 00a0 0000 0000
b: 3042 0000 0000 0000
c: 0082 00a0 0000 0000
d: 3042 0000 0000 0000

macOSはシステムのデフォルトがUTF8,日本語版WindowsはCP932となっているのがわかる。

swift5.9(Xcode15)で気になる機能

Swift.org - Swift 5.9 Released

SE-0366: consume operator to end the lifetime of a variable binding.
C++の std::move と同じものが導入されました。

swift-evolution/proposals/0366-move-function.md at main · apple/swift-evolution · GitHub

SE-0380: if and switch expressions

switchのreturn書くのが面倒だったのが改善されました。
2項演算子の可読性が悪いのも改善できます。

swift-evolution/proposals/0380-if-switch-expressions.md at main · apple/swift-evolution · GitHub

SE-0390: Noncopyable structs and enums

C++の std::unique_ptr と同じものが導入されました。

swift-evolution/proposals/0390-noncopyable-structs-and-enums.md at main · apple/swift-evolution · GitHub

githubに登録した公開鍵がどれか確認する方法

githubに登録したSSHの公開鍵はアカウントの設定のところに情報がリストされますが、 作成時に使ったメールアドレスと SHA256:D60Uldj(以下略) 追加日時などしか表示されません。
一方自分が持つ公開鍵にはSHA256:のような情報はありません。
メールアドレスを何度も使い回している場合はこの公開鍵がどれなのか区別がつかないのですが、確認する方法がわかりました。

このSHA256:D60Uldj...のものは公開鍵のフィンガープリントと呼ばれ、単純に公開鍵のSHA256ハッシュを取ったもののようです。手元に公開鍵ファイルがあればssh-keygenコマンドで簡単にフィンガープリントが計算できるようです。
$ ssh-keygen -l -f xxx.pub
256 SHA256:D60Ulfj... hoge@fuga... (ED25519)

github上に表示されているフィンガープリントと一致しました。