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/.
12 from libreoffice
.util
.compatibility
import use_lazy_string
14 class StringPrinterHelper(object):
15 '''Base for all string pretty printers'''
17 class MustBeImplemented(Exception):
20 def __init__(self
, typename
, val
, encoding
= None):
21 self
.typename
= typename
23 self
.encoding
= encoding
29 return self
.make_string(data
, self
.encoding
, len)
31 return "uninitialized %s" % self
.typename
33 def display_hint(self
):
43 raise self
.MustBeImplemented()
49 def make_string(data
, encoding
= None, length
= -1):
50 '''Creates a new string from memory'''
55 # we need to determine length, if not given (for sal_Unicode*)
58 while data
[length
] != 0 and length
<= 512: # arbitrary limit
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
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: