Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goの文字列置換とその実装

po3rin
July 14, 2020

 Goの文字列置換とその実装

文字列置換が func Replace と func (*Replacer) Replace の2つの標準機能のアルゴリズムやデータ構造の違いからなぜ2つあるのかを理解しよう。

po3rin

July 14, 2020
Tweet

More Decks by po3rin

Other Decks in Programming

Transcript

  1. (Pͷจࣈྻஔ׵ͱͦͷ࣮૷
    'VLVPLBHP
    +VMZ
    CZQPSJO
    (PTUSJOHSFQMBDFNFOUBOEJUTJNQMFNFOUBUJPO

    View full-size slide

  2. pon ( @po3rin )

    View full-size slide

  3. Introduction
    \

    View full-size slide

  4. (PʹΑΔจࣈྻஔ׵͸ਖ਼نදݱΛൈ͍ͯ̎ͭͷํ๏͕ఏڙ͞Ε͍ͯΔ
    3FQMBDFJO(P

    View full-size slide

  5. GVOD3FQMBDF
    GVOD 3FQMBDFS
    3FQMBDF
    3FQMBDFJO(P

    View full-size slide

  6. จࣈྻஔ׵͕GVOD3FQMBDFͱGVOD 3FQMBDFS
    3FQMBDFͷ̎ͭ͋Δͷ͔ʁ
    ಺෦ͷΞϧΰϦζϜ΍σʔλߏ଄ͷҧ͍͔Βͳͥͭ͋Δͷ͔Λཧղ͠Α͏ɻ
    3FQMBDFJO(P

    View full-size slide

  7. func Replace
    \

    View full-size slide

  8. ࣮͸GVOD3FQMBDF͸TUSJOHT*OEFY͑͋͞Ε͹૊ΈཱͯΕΔɻ
    GVOD3FQMBDF

    View full-size slide

  9. TUSJOHT*OEFY͸Tͷத͔ΒTVCTUS͕࠷ॳʹग़ݱ͢ΔҐஔΛฦ͢
    GVOD3FQMBDF

    View full-size slide

  10. ӈ͸ΤοδέʔεΛམͱͨ͠ίʔυɻ
    <>CZUFͱDPQZͱTUSJOHT*OEFYΛۦ
    ࢖͍ͯ͠Δͷ͕Θ͔Δ
    GVOD3FQMBDF

    View full-size slide

  11. GVOD3FQMBDF͕ಡΜͰ͍Δ
    TUSJOHT$PVOU΋TUSJOHT*OEFYʹґଘ
    ͍ͯ͠Δɻ
    GVOD3FQMBDF

    View full-size slide

  12. ͭ·ΓGVOD3FQMBDF͸TUSJOHT*OEFY͑͋͞Ε͹૊ΈཱͯΕΔɻ
    GVOD3FQMBDF

    View full-size slide

  13. TUSJOHT*OEFY͸#SVUFGPSDF΋͘͠͸3BCJOLBSQͰTVCTUSͷҐஔΛಛఆ͍ͯ͠Δɻ
    ৄ͍͠ղઆ͸ڈ೥ͷΞυϕϯτΧϨϯμʔʹ౤ߘͨ͠ͷͰੋඇʂ
    IUUQTQPSJODPNCMPHHPSBCJOLBSQ
    GVOD3FQMBDF

    View full-size slide

  14. ࣮͸GVOD3FQMBDF͸TUSJOHT*OEFY͑͋͞Ε͹૊ΈཱͯΕΔɻ
    TUSJOHT*OEFY͸࠷௿ճݺͿ
    ग़ݱճ਺ͷऔಘʹݺͼग़͢
    ஔ׵ॲཧ࣌ʹͦͷճ਺෼ݺͼग़͢
    TUSJOHT*OEFY͸#SVUFGPSDF΋͘͠͸3BCJOLBSQΞϧΰϦζϜΛར༻͍ͯ͠Δ
    GVOD3FQMBDFͷ·ͱΊ

    View full-size slide

  15. func (*Replacer) Replace
    \

    View full-size slide

  16. GVOD 3FQMBDFS
    3FQMBDF͕ੜ͍͑ͯΔSͰ͸಺෦࣮૷ΛೖΕସ͑ΔͨΊʹSFQMBDFS
    JOUFSGBDFͰந৅Խ͍ͯ͠ΔɻPMEOFX͸ޙͰ΋ৄ֬͘͠ೝ͠·͕͢ɺஔ׵͍ͨ͠จࣈྻͷϖ
    ΞͷϦετΛอ࣋͠·͢ɻ
    GVOD 3FQMBDFS
    3FQMBDF

    View full-size slide

  17. 3FQMBDFSJOUFSGBDFΛ࣮૷͢Δίϯϙ
    δοτܕ͸छྨɻVTFSʹ౉͞ΕͨҾ਺ʹ
    Αͬͯग़͠෼͚Δ
    GVOD 3FQMBDFS
    3FQMBDF

    View full-size slide

  18. ஔ׵ϖΞΛ֨ೲ͢ΔPMEOFXʹ࠷దͳSFQMBDFS͕બ͹ΕΔ
    GVOD 3FQMBDFS
    3FQMBDF

    View full-size slide

  19. ࠓճ͸ΑΓҰൠతͳSFQMBDFSͰ͋ΔHFOFSJD3FQMBDFSΛݟ͍ͯ͘ɻ಺෦ͰUSJF/PEFͳΔ΋ͷ͕͋Δɻ
    GVOD 3FQMBDFS
    3FQMBDF

    View full-size slide

  20. BY
    BZ
    CDCD
    Y
    YZ

    B C Y
    Y Z DCD Z
    (PͰ͸TUSJOHTύοέʔδͰஔ׵ʹ࢖͏5SJF͕࣮૷͞Ε͍ͯΔ
    5SJFߏங
    $PNQMFUF,FZ
    /PEF
    5SJF

    View full-size slide

  21. FF

    B C Y
    Y Z DCD Z
    (Pͷ5SJFͰ͸$PNQMFUF,FZʹஔ׵ઌͷจࣈྻΛ֨ೲ͍ͯ͠Δ
    $PNQMFUF,FZ
    /PEF
    5SJF

    BB CC DD EE

    View full-size slide

  22. ԼهͷྫͰ͸3FQMBDFͷҾ਺ͷจࣈྻʮXSJUFT(PMBOHBOE
    $MBOHʯΛҰจࣈͣͭ5SJFΛ࢖ͬͯݕূ͍ͯ͘͠ɻ5SJFͰூͬͯ
    ͍ͬͯҰக͠ͳ͔ͬͨ෼͸࣍ͷ-PPL6QͰ͸ඈ͹͢ɻ

    $ (
    MBOH PMBOH
    $ (P
    5SJF

    View full-size slide

  23. 5SJFΛMPPLVQ͍ͯ͠Δ
    8SJUF4USJOHͰΘ͔Δ͕ɺෳ਺ͷஔ
    ׵ϖΞ͕͋ͬͯ΋ର৅จࣈྻશମ
    ͷ૸ࠪ͸ճͰऴΘΔɺ
    5SJF

    View full-size slide

  24. Ҿ਺࣍ୈͰSFQMBDFSΛ࢖͍෼͚͍ͯΔɻ
    HFOFSJD3FQMBDFS಺෦Ͱ͸5SJFΛར༻͍ͯ͠Δɻ
    ஔ׵Ϧετ͕ෳ਺͋ͬͯ΋ର৅จࣈྻͷ૸ࠪ͸ճͰࡁΉ
    GVOD 3FQMBDFS
    3FQMBDFͷ·ͱΊ

    View full-size slide

  25. ಺෦ߏ଄Λ஌ΔͱԾઆཱ͕ͯΕΔ
    ͜͜·Ͱͷ஌͕ࣝ͋Ε͹ͲͪΒΛ࢖͑͹ྑ͍͔ɺͲ͏࢖͑͹ྑ͍ͷ͔ͷԾઆཱ͕ͯΕ·͢ɻ
    ஔ׵ϖΞ͕̍ͭͳΒ5SJFΛߏங͢Δίετ͕͋ΔͷͰ3FQMBDFͰे෼Ͱ͸ͳ͍͔ʁ
    ஔ׵ϖΞ͕ଟ͍ͳΒจࣈྻશମͷ૸͕ࠪճͰ͓ΘΔ3FQMBDFSΛ࢖͏΂͖Ͱ͸
    ஔ׵ର৅ͷจࣈྻ͕௕͍PSଟ͍ͱ5SJFߏஙʹ͕͔͔࣌ؒΔͷͰJOJUͰݺͿ΂͖Ͱ͸ʁ
    ର৅จࣈྻͷ௕͕͞ࢧ഑తͳΒ૸ࠪҰճͷ3FQMBDFͷํ͕ྑ͍ʁ

    View full-size slide

  26. GVOD3FQMBDFͱGVOD 3FQMBDFS
    3FQMBDFͷ಺෦࣮૷Λগ͠ղઆͨ͠
    ࣅͨײ͡ͷؔ਺͕͋ͬͯ΋಺෦࣮૷ΛݟΔͱશવҧ͏ɻ
    ಺෦ͷΞϧΰϦζϜ΍σʔλߏ଄͕Θ͔ΔͱύϑΥʔϚϯεଌఆͷ݁Ռͷೲಘ౓͕
    มΘΔɻ
    ·ͱΊ

    View full-size slide

  27. QPOͷߟ͑ΔίʔυϦʔσΟϯάͷ5JQT
    ·ͣ͸Τοδέʔε͸ແࢹ
    ෳࡶͳ಺෦ؔ਺͸(P1MBZHSPVOEʹίϐϖͯ֓͠ܗΛัΒ͑Δ

    View full-size slide