1 //===- PackedVersion.cpp --------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implements the Mach-O packed version.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/TextAPI/MachO/PackedVersion.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
23 bool PackedVersion::parse32(StringRef Str
) {
29 SmallVector
<StringRef
, 3> Parts
;
30 SplitString(Str
, Parts
, ".");
35 unsigned long long Num
;
36 if (getAsUnsignedInteger(Parts
[0], 10, Num
))
44 for (unsigned i
= 1, ShiftNum
= 8; i
< Parts
.size(); ++i
, ShiftNum
-= 8) {
45 if (getAsUnsignedInteger(Parts
[i
], 10, Num
))
51 Version
|= (Num
<< ShiftNum
);
57 std::pair
<bool, bool> PackedVersion::parse64(StringRef Str
) {
58 bool Truncated
= false;
62 return std::make_pair(false, Truncated
);
64 SmallVector
<StringRef
, 5> Parts
;
65 SplitString(Str
, Parts
, ".");
68 return std::make_pair(false, Truncated
);
70 unsigned long long Num
;
71 if (getAsUnsignedInteger(Parts
[0], 10, Num
))
72 return std::make_pair(false, Truncated
);
74 if (Num
> 0xFFFFFFULL
)
75 return std::make_pair(false, Truncated
);
77 if (Num
> 0xFFFFULL
) {
83 for (unsigned i
= 1, ShiftNum
= 8; i
< Parts
.size() && i
< 3;
85 if (getAsUnsignedInteger(Parts
[i
], 10, Num
))
86 return std::make_pair(false, Truncated
);
89 return std::make_pair(false, Truncated
);
95 Version
|= (Num
<< ShiftNum
);
101 return std::make_pair(true, Truncated
);
104 void PackedVersion::print(raw_ostream
&OS
) const {
105 OS
<< format("%d", getMajor());
106 if (getMinor() || getSubminor())
107 OS
<< format(".%d", getMinor());
109 OS
<< format(".%d", getSubminor());
112 } // end namespace MachO.
113 } // end namespace llvm.