tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / solenv / gdb / libreoffice / util / string.py
blob742aabbaca2750e9e1adc1a518aee3a73adf0650
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import gdb
12 from libreoffice.util.compatibility import use_lazy_string
14 class StringPrinterHelper(object):
15 '''Base for all string pretty printers'''
17 class MustBeImplemented(Exception):
18 pass
20 def __init__(self, typename, val, encoding = None):
21 self.typename = typename
22 self.val = val
23 self.encoding = encoding
25 def to_string(self):
26 if self.valid():
27 data = self.data()
28 len = self.length()
29 return self.make_string(data, self.encoding, len)
30 else:
31 return "uninitialized %s" % self.typename
33 def display_hint(self):
34 if self.valid():
35 return 'string'
36 else:
37 return None
39 def valid(self):
40 return True
42 def data(self):
43 raise self.MustBeImplemented()
45 def length(self):
46 return -1
48 @staticmethod
49 def make_string(data, encoding = None, length = -1):
50 '''Creates a new string from memory'''
52 if not encoding:
53 encoding = ''
55 # we need to determine length, if not given (for sal_Unicode*)
56 if length < 0:
57 length = 0
58 while data[length] != 0 and length <= 512: # arbitrary limit
59 length += 1
61 if use_lazy_string:
62 return data.lazy_string(encoding, length)
64 # The gdb.Value.string() conversion works on array of bytes, but
65 # the length we have is the length of the string. So we must
66 # multiply it by width of character if the string is Unicode.
67 width = data[0].type.sizeof
68 if width > 1:
69 length = length * width
71 char = gdb.lookup_type('char')
72 bytes = data.cast(char.pointer())
73 return bytes.string(encoding, length = length)
75 # vim:set shiftwidth=4 softtabstop=4 expandtab: