| OLD | NEW |
| 1 // Copyright 2003, 2004 Colin Percival | 1 // Copyright 2003, 2004 Colin Percival |
| 2 // All rights reserved | 2 // All rights reserved |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted providing that the following conditions | 5 // modification, are permitted providing that the following conditions |
| 6 // are met: | 6 // are met: |
| 7 // 1. Redistributions of source code must retain the above copyright | 7 // 1. Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. | 8 // notice, this list of conditions and the following disclaimer. |
| 9 // 2. Redistributions in binary form must reproduce the above copyright | 9 // 2. Redistributions in binary form must reproduce the above copyright |
| 10 // notice, this list of conditions and the following disclaimer in the | 10 // notice, this list of conditions and the following disclaimer in the |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // http://www.larsson.dogma.net/qsufsort.c | 41 // http://www.larsson.dogma.net/qsufsort.c |
| 42 // --Samuel Huang <huangs@chromium.org> | 42 // --Samuel Huang <huangs@chromium.org> |
| 43 | 43 |
| 44 // Copyright 2016 The Chromium Authors. All rights reserved. | 44 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 45 // Use of this source code is governed by a BSD-style license that can be | 45 // Use of this source code is governed by a BSD-style license that can be |
| 46 // found in the LICENSE file. | 46 // found in the LICENSE file. |
| 47 | 47 |
| 48 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ | 48 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ |
| 49 #define COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ | 49 #define COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ |
| 50 | 50 |
| 51 #include <algorithm> | |
| 52 #include <cstring> | |
| 53 | |
| 54 namespace courgette { | 51 namespace courgette { |
| 55 namespace qsuf { | 52 namespace qsuf { |
| 56 | 53 |
| 57 // ------------------------------------------------------------------------ | 54 // ------------------------------------------------------------------------ |
| 58 // | 55 // |
| 59 // The following code is taken verbatim from 'bsdiff.c'. Please keep all the | 56 // The following code is taken verbatim from 'bsdiff.c'. Please keep all the |
| 60 // code formatting and variable names. The changes from the original are: | 57 // code formatting and variable names. The changes from the original are: |
| 61 // (1) replacing tabs with spaces, | 58 // (1) replacing tabs with spaces, |
| 62 // (2) indentation and spacing, | 59 // (2) indentation and spacing, |
| 63 // (3) using 'const', | 60 // (3) using 'const', |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 }; | 210 }; |
| 214 }; | 211 }; |
| 215 if (len) | 212 if (len) |
| 216 I[i - len] = -len; | 213 I[i - len] = -len; |
| 217 }; | 214 }; |
| 218 | 215 |
| 219 for (i = 0; i < oldsize + 1; i++) | 216 for (i = 0; i < oldsize + 1; i++) |
| 220 I[V[i]] = i; | 217 I[V[i]] = i; |
| 221 } | 218 } |
| 222 | 219 |
| 223 static int matchlen(const unsigned char* old, | |
| 224 int oldsize, | |
| 225 const unsigned char* newbuf, | |
| 226 int newsize) { | |
| 227 int i; | |
| 228 | |
| 229 for (i = 0; (i < oldsize) && (i < newsize); i++) | |
| 230 if (old[i] != newbuf[i]) | |
| 231 break; | |
| 232 | |
| 233 return i; | |
| 234 } | |
| 235 | |
| 236 template <class T> | |
| 237 static int search(T I, | |
| 238 const unsigned char* old, | |
| 239 int oldsize, | |
| 240 const unsigned char* newbuf, | |
| 241 int newsize, | |
| 242 int* pos) { | |
| 243 int lo = 0; | |
| 244 int hi = oldsize; | |
| 245 while (hi - lo >= 2) { | |
| 246 int mid = (lo + hi) / 2; | |
| 247 if (memcmp(old + I[mid], newbuf, std::min(oldsize - I[mid], newsize)) < 0) { | |
| 248 lo = mid; | |
| 249 } else { | |
| 250 hi = mid; | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 int x = matchlen(old + I[lo], oldsize - I[lo], newbuf, newsize); | |
| 255 int y = matchlen(old + I[hi], oldsize - I[hi], newbuf, newsize); | |
| 256 if (x > y) { | |
| 257 *pos = I[lo]; | |
| 258 return x; | |
| 259 } | |
| 260 *pos = I[hi]; | |
| 261 return y; | |
| 262 } | |
| 263 | |
| 264 // End of 'verbatim' code. | 220 // End of 'verbatim' code. |
| 265 // ------------------------------------------------------------------------ | 221 // ------------------------------------------------------------------------ |
| 266 | 222 |
| 267 } // namespace qsuf | 223 } // namespace qsuf |
| 268 } // namespace courgette | 224 } // namespace courgette |
| 225 |
| 269 #endif // COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ | 226 #endif // COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ |
| OLD | NEW |