]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/subversion/subversion/libsvn_subr/eol.c
Merge LLDB 3.8
[FreeBSD/FreeBSD.git] / contrib / subversion / subversion / libsvn_subr / eol.c
1 /*
2  * eol.c :  generic eol/keyword routines
3  *
4  * ====================================================================
5  *    Licensed to the Apache Software Foundation (ASF) under one
6  *    or more contributor license agreements.  See the NOTICE file
7  *    distributed with this work for additional information
8  *    regarding copyright ownership.  The ASF licenses this file
9  *    to you under the Apache License, Version 2.0 (the
10  *    "License"); you may not use this file except in compliance
11  *    with the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *    Unless required by applicable law or agreed to in writing,
16  *    software distributed under the License is distributed on an
17  *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  *    KIND, either express or implied.  See the License for the
19  *    specific language governing permissions and limitations
20  *    under the License.
21  * ====================================================================
22  */
23
24
25 \f
26 #define APR_WANT_STRFUNC
27
28 #include <apr_file_io.h>
29 #include "svn_io.h"
30 #include "private/svn_eol_private.h"
31 #include "private/svn_dep_compat.h"
32
33 char *
34 svn_eol__find_eol_start(char *buf, apr_size_t len)
35 {
36 #if !SVN_UNALIGNED_ACCESS_IS_OK
37
38   /* On some systems, we need to make sure that BUF is properly aligned
39    * for chunky data access. This overhead is still justified because
40    * only lines tend to be tens of chars long.
41    */
42   for (; (len > 0) && ((apr_uintptr_t)buf) & (sizeof(apr_uintptr_t)-1)
43        ; ++buf, --len)
44   {
45     if (*buf == '\n' || *buf == '\r')
46       return buf;
47   }
48
49 #endif
50
51   /* Scan the input one machine word at a time. */
52   for (; len > sizeof(apr_uintptr_t)
53        ; buf += sizeof(apr_uintptr_t), len -= sizeof(apr_uintptr_t))
54   {
55     /* This is a variant of the well-known strlen test: */
56     apr_uintptr_t chunk = *(const apr_uintptr_t *)buf;
57
58     /* A byte in SVN__R_TEST is \0, iff it was \r in *BUF.
59      * Similarly, SVN__N_TEST is an indicator for \n. */
60     apr_uintptr_t r_test = chunk ^ SVN__R_MASK;
61     apr_uintptr_t n_test = chunk ^ SVN__N_MASK;
62
63     /* A byte in SVN__R_TEST can only be < 0x80, iff it has been \0 before
64      * (i.e. \r in *BUF). Ditto for SVN__N_TEST. */
65     r_test |= (r_test & SVN__LOWER_7BITS_SET) + SVN__LOWER_7BITS_SET;
66     n_test |= (n_test & SVN__LOWER_7BITS_SET) + SVN__LOWER_7BITS_SET;
67
68     /* Check whether at least one of the words contains a byte <0x80
69      * (if one is detected, there was a \r or \n in CHUNK). */
70     if ((r_test & n_test & SVN__BIT_7_SET) != SVN__BIT_7_SET)
71       break;
72   }
73
74   /* The remaining odd bytes will be examined the naive way: */
75   for (; len > 0; ++buf, --len)
76     {
77       if (*buf == '\n' || *buf == '\r')
78         return buf;
79     }
80
81   return NULL;
82 }
83
84 const char *
85 svn_eol__detect_eol(char *buf, apr_size_t len, char **eolp)
86 {
87   char *eol;
88
89   eol = svn_eol__find_eol_start(buf, len);
90   if (eol)
91     {
92       if (eolp)
93         *eolp = eol;
94
95       if (*eol == '\n')
96         return "\n";
97
98       /* We found a CR. */
99       ++eol;
100       if (eol == buf + len || *eol != '\n')
101         return "\r";
102       return "\r\n";
103     }
104
105   return NULL;
106 }