]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
Move UTF-8 char length routine into separate function.
[lilypond.git] / lily / misc.cc
1 /*
2   misc.cc -- implement various stuff
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10
11 #include "misc.hh"
12 #include "warn.hh"
13
14 /*
15   Return the 2-log, rounded down
16 */
17 int
18 intlog2 (int d)
19 {
20   assert (d);
21   int i = 0;
22   while ((d != 1))
23     {
24       d /= 2;
25       i++;
26     }
27
28   assert (! (d / 2));
29   return i;
30 }
31
32 double
33 log_2 (double x)
34 {
35   return log (x) / log (2.0);
36 }
37
38 Real
39 directed_round (Real f, Direction d)
40 {
41   if (d < 0)
42     return floor (f);
43   else
44     return ceil (f);
45 }
46
47
48
49 /*
50    0 at threshold,  1 at 0, with 1/x falloff.
51  */
52 Real
53 peak_around (Real epsilon,  Real threshold, Real x)
54 {
55   if (x < 0)
56     return 1.0;
57   return max (- epsilon * (x - threshold) / ((x + epsilon) * threshold), 0.0);
58 }
59
60 /*
61   0 at 0,  1 at standard_x, and increasing thereafter. 
62  */
63 Real
64 convex_amplifier (Real standard_x, Real increase_factor, Real x)
65 {
66   return (exp (increase_factor * x / standard_x) - 1.0) / (exp (increase_factor) - 1.0); 
67 }
68
69 string
70 camel_case_to_lisp_identifier (string in)
71 {
72   vector<char> out;
73   
74   /* don't add '-' before first character */
75   out.push_back (char (tolower (in[0])));
76     
77   for (size_t inpos = 1; inpos < in.size (); inpos++)
78     {
79       if (isupper (in[inpos]))
80         out.push_back ('-');
81       out.push_back ( char(tolower (in[inpos])));
82     }
83   
84   string result (&out[0], out.size ());
85   replace_all (&result, '_', '-');
86
87   return result;
88 }
89
90 vsize
91 utf8_char_len (char current)
92 {
93   vsize char_len = 1;
94
95   // U+10000 - U+10FFFF
96   if ((current & 0xF0) == 0xF0)
97     char_len = 4;
98   // U+0800 - U+FFFF
99   else if ((current & 0xE0) == 0xE0)
100     char_len = 3;
101   // U+0080 - U+07FF
102   else if ((current & 0xC0) == 0xC0)
103     char_len = 2;
104   else if (current & 0x80)
105     programming_error ("invalid UTF-8 string");
106
107   return char_len;
108 }