1 //===-- ThisThread.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Error.h"
11 #include "lldb/Host/HostInfo.h"
12 #include "lldb/Host/ThisThread.h"
14 #include "llvm/ADT/STLExtras.h"
19 using namespace lldb_private;
22 ThisThread::SetName(llvm::StringRef name, int max_length)
24 std::string truncated_name(name.data());
26 // Thread names are coming in like '<lldb.comm.debugger.edit>' and
27 // '<lldb.comm.debugger.editline>'. So just chopping the end of the string
28 // off leads to a lot of similar named threads. Go through the thread name
29 // and search for the last dot and use that.
31 if (max_length > 0 && truncated_name.length() > static_cast<size_t>(max_length))
33 // First see if we can get lucky by removing any initial or final braces.
34 std::string::size_type begin = truncated_name.find_first_not_of("(<");
35 std::string::size_type end = truncated_name.find_last_not_of(")>.");
36 if (end - begin > static_cast<size_t>(max_length))
38 // We're still too long. Since this is a dotted component, use everything after the last
39 // dot, up to a maximum of |length| characters.
40 std::string::size_type last_dot = truncated_name.rfind('.');
41 if (last_dot != std::string::npos)
44 end = std::min(end, begin + max_length);
47 std::string::size_type count = end - begin + 1;
48 truncated_name = truncated_name.substr(begin, count);
51 SetName(truncated_name.c_str());