<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/91634>91634</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            std::string_view::substr triggers unnecessary ctor safety checks in libc++ safe mode
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          davidben
      </td>
    </tr>
</table>

<pre>
    std::string_view checks that the input length is less than `PTRDIFF_MAX`, which is useful for caching accidental negative numbers getting in there.

But the cost is that the various methods of `string_view` trigger this check too, because the compiler doesn't know that `sv.size() <= PTRDIFF_MAX`. See this godbolt:
https://godbolt.org/z/zsG4rbTdb

We could fix this with some assumes, but Clang cannot do this analysis anyway (see #91619). So I think probably the simplest option is to add a private ctor, `string_view(ptr, len, __assume_valid)` and make the internal calls use that one.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsU12vnDYQ_TXel1EQa9gPHni4uVdb5aFS1URq31aDPYB7jY08w243v74ybJo0qgQYPMOZc45nkNkNgahVh4_q8LbDRcaYWos3ZzsKuy7aR8tiVfWiqheW5MJwvTm6gxnJvDPIiAIyErgwLwKewiAjOAZPvEYDqGP525ff3z5dLtdfX_5Ux1LpV7iPzqx5C1O_eOhjAoNmdGEANMZZCoIeAg0o7kYQlqmjxDCQSM5xIVdNVKjyTZUv2_PjsnExkSVj_0vuhsnFhWEiGaNliH1m9YMcdSxBkhsGSiCj400eSIyZbEcGF6Yn9jQ7TwlsJA5KnwTeQ7xvtTLorWD3lZQ-K92Aql5V9Qb_1V_AZ6KtzBBtF71ke1cFo8jM-UtflL48o0VMg9KXr_nmX-rUfbHdj7L_yKwWb6F3f2-wdycjcJwIkHmZiFcVi8CrxzCAwRCigI1bNgb0D15fHnd8gNJnJgKlq2Z_3DdKNwV8jvApZ4d3mFPssPOP1Q520-yJBeIsLobV9QhoLSDMyd1QCIzElAn8ZLk-z7Luewp5uV43stcbemeVbvKhYLAw4Ts9e0woBfRg0Pu1dTbbY6BiZ9vKNlWDO2r3p_1Bn071sd6NLfWm6bE8VdgfbNWcqT_qsjK2pL4sUVc71-pS1-WhbPanw6mui_PhYEnrru8QT1ifVV3ShM4X3t-mfBg7x7xQ2-yPVb3z2JHndYC09q4zSn9cL50HKrX5pw_dMrCqS-9Y-DuMOPH0v-P13Fg6lvStMRmWEMgQM6bHaiow9iSPb7PoAnyvv8ZgipZ2S_LtT43lZFy6wsRJ6Uum81w-zCn-RUaUvqwSWenLqvKfAAAA___FmmEo">