llvm.org GIT mirror llvm / b357e06
Introduce Twine::toStringRef, a variant of toVector which avoids the copy if the twine can be represented as a single StringRef. Use the new methode to simplify some twine users. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93317 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 10 years ago
5 changed file(s) with 24 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
374374 case StringRefKind: return *(const StringRef*)LHS;
375375 }
376376 }
377
378
377
378 /// toStringRef - This returns the twine as a single StringRef if it can be
379 /// represented as such. Otherwise the twine is written into the given
380 /// SmallVector and a StringRef to the SmallVector's data is returned.
381 StringRef toStringRef(SmallVectorImpl &Out) const;
382
379383 /// print - Write the concatenated string represented by this twine to the
380384 /// stream \arg OS.
381385 void print(raw_ostream &OS) const;
1414
1515 std::string Twine::str() const {
1616 SmallString<256> Vec;
17 toVector(Vec);
18 return std::string(Vec.begin(), Vec.end());
17 return toStringRef(Vec).str();
1918 }
2019
2120 void Twine::toVector(SmallVectorImpl &Out) const {
2221 raw_svector_ostream OS(Out);
2322 print(OS);
23 }
24
25 StringRef Twine::toStringRef(SmallVectorImpl &Out) const {
26 if (isSingleStringRef())
27 return getSingleStringRef();
28 toVector(Out);
29 return StringRef(Out.data(), Out.size());
2430 }
2531
2632 void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
4040 const Twine &TheName,
4141 ManglerPrefixTy PrefixTy) {
4242 SmallString<256> TmpData;
43 TheName.toVector(TmpData);
44 StringRef X = TmpData.str();
43 StringRef X = TheName.toStringRef(TmpData);
4544 assert(!X.empty() && "Cannot mangle empty strings");
4645
4746 if (!UseQuotes) {
187186 void Mangler::getNameWithPrefix(SmallVectorImpl &OutName,
188187 const Twine &GVName, ManglerPrefixTy PrefixTy) {
189188 SmallString<256> TmpData;
190 StringRef Name;
191 if (GVName.isSingleStringRef())
192 Name = GVName.getSingleStringRef();
193 else {
194 GVName.toVector(TmpData);
195 Name = TmpData.str();
196 }
189 StringRef Name = GVName.toStringRef(TmpData);
197190 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
198191
199192 // If the global name is not led with \1, add the appropriate prefixes.
326326 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
327327
328328 SmallString<256> NameData;
329 NewName.toVector(NameData);
330
331 const char *NameStr = NameData.data();
332 unsigned NameLen = NameData.size();
333
334 StringRef NameRef = StringRef(NameStr, NameLen);
329 StringRef NameRef = NewName.toStringRef(NameData);
330
335331 // Name isn't changing?
336332 if (getName() == NameRef)
337333 return;
169169 return;
170170
171171 SmallString<256> NameData;
172 NewName.toVector(NameData);
173
174 const char *NameStr = NameData.data();
175 unsigned NameLen = NameData.size();
172 StringRef NameRef = NewName.toStringRef(NameData);
176173
177174 // Name isn't changing?
178 if (getName() == StringRef(NameStr, NameLen))
175 if (getName() == NameRef)
179176 return;
180177
181178 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
186183 return; // Cannot set a name on this value (e.g. constant).
187184
188185 if (!ST) { // No symbol table to update? Just do the change.
189 if (NameLen == 0) {
186 if (NameRef.empty()) {
190187 // Free the name for this value.
191188 Name->Destroy();
192189 Name = 0;
200197 // then reallocated.
201198
202199 // Create the new name.
203 Name = ValueName::Create(NameStr, NameStr+NameLen);
200 Name = ValueName::Create(NameRef.begin(), NameRef.end());
204201 Name->setValue(this);
205202 return;
206203 }
213210 Name->Destroy();
214211 Name = 0;
215212
216 if (NameLen == 0)
213 if (NameRef.empty())
217214 return;
218215 }
219216
220217 // Name is changing to something new.
221 Name = ST->createValueName(StringRef(NameStr, NameLen), this);
218 Name = ST->createValueName(NameRef, this);
222219 }
223220
224221