java.util.UUID.fromString not checking length

后端 未结 5 1097
深忆病人
深忆病人 2021-01-17 09:34

When I looked into the implementation of java.util.UUID.fromString, I found that it doesn\'t check for the UUID length. Is there any particular reason for this?

5条回答
  •  抹茶落季
    2021-01-17 10:26

    Only the original authors of the UUID class could tell you why they chose not to check the component lengths in the fromString method, but I suspect they were trying to heed Postel's law:

    Be liberal in what you accept, and conservative in what you send.

    You can always check the input against a regular expression like this one:

    [0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}
    

提交回复
热议问题