Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/secore/marshall.py
blob: ebcc71d4bc658538a68985e4a8239c1c1149fa96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
#
# Copyright (C) 2007 Lemur Consulting Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
r"""marshall.py: Marshal values into strings

"""
__docformat__ = "restructuredtext en"

import math

def _long_to_base256_array(value, length, flip):
    result = []
    for i in xrange(length):
        n = value % 256
        if flip: n = 255 - n
        result.insert(0, chr(n))
        value /= 256
    return result

def float_to_string(value):
    """Marshall a floating point number to a string which sorts in the
    appropriate manner.

    """
    mantissa, exponent = math.frexp(value)
    sign = '1'
    if mantissa < 0:
        mantissa = -mantissa
        sign = '0'

    # IEEE representation of doubles uses 11 bits for the exponent, with a bias
    # of 1023.  There's then another 52 bits in the mantissa, so we need to
    # add 1075 to be sure that the exponent won't be negative.
    # Even then, we check that the exponent isn't negative, and consider the
    # value to be equal to zero if it is.
    exponent += 1075
    if exponent < 0: # Note - this can't happen on most architectures #pragma: no cover
        exponent = 0
        mantissa = 0
    elif mantissa == 0:
        exponent = 0

    # IEEE representation of doubles uses 52 bits for the mantissa.  Convert it
    # to a 7 character string, and convert the exponent to a 2 character
    # string.

    mantissa = long(mantissa * (2**52))

    digits = [sign]
    digits.extend(_long_to_base256_array(exponent, 2, sign == '0'))
    digits.extend(_long_to_base256_array(mantissa, 7, sign == '0'))

    return ''.join(digits)

def date_to_string(date):
    """Marshall a date to a string which sorts in the appropriate manner.

    """
    return '%04d%02d%02d' % (date.year, date.month, date.day)