| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2003, 2004 Colin Percival |
| 2 // All rights reserved |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted providing that the following conditions |
| 6 // are met: |
| 7 // 1. Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. |
| 9 // 2. Redistributions in binary form must reproduce the above copyright |
| 10 // notice, this list of conditions and the following disclaimer in the |
| 11 // documentation and/or other materials provided with the distribution. |
| 12 // |
| 13 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 18 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 19 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 20 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 21 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 22 // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 // POSSIBILITY OF SUCH DAMAGE. |
| 24 // |
| 25 // For the terms under which this work may be distributed, please see |
| 26 // the adjoining file "LICENSE". |
| 27 // |
| 28 // ChangeLog: |
| 29 // 2005-05-05 - Use the modified header struct from bspatch.h; use 32-bit |
| 30 // values throughout. |
| 31 // --Benjamin Smedberg <benjamin@smedbergs.us> |
| 32 // 2015-08-03 - Change search() to template to allow PagedArray usage. |
| 33 // --Samuel Huang <huangs@chromium.org> |
| 34 // 2015-08-19 - Optimized search() to be non-recursive. |
| 35 // --Samuel Huang <huangs@chromium.org> |
| 36 // 2016-06-17 - Moved matchlen() and search() to a new file; format; changed |
| 37 // search() use std::lexicographical_compare(). |
| 38 // --Samuel Huang <huangs@chromium.org> |
| 39 |
| 40 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 41 // Use of this source code is governed by a BSD-style license that can be |
| 42 // found in the LICENSE file. |
| 43 |
| 44 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ |
| 45 #define COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ |
| 46 |
| 47 #include <algorithm> |
| 48 #include <cstring> |
| 49 |
| 50 namespace courgette { |
| 51 |
| 52 // Similar to ::memcmp(), but returns match length instead. |
| 53 inline int matchlen(const unsigned char* old, |
| 54 int oldsize, |
| 55 const unsigned char* newbuf, |
| 56 int newsize) { |
| 57 int i; |
| 58 |
| 59 for (i = 0; (i < oldsize) && (i < newsize); i++) |
| 60 if (old[i] != newbuf[i]) |
| 61 break; |
| 62 |
| 63 return i; |
| 64 } |
| 65 |
| 66 // Finds a suffix in |old| that has the longest common prefix with |newbuf|, |
| 67 // aided by suffix array |I| of |old|. Returns the match length, and writes to |
| 68 // |pos| a position of best match in |old|. If multiple such positions exist, |
| 69 // |pos| would take an arbitrary one. |
| 70 template <class T> |
| 71 int search(T I, |
| 72 const unsigned char* old, |
| 73 int oldsize, |
| 74 const unsigned char* newbuf, |
| 75 int newsize, |
| 76 int* pos) { |
| 77 int lo = 0; |
| 78 int hi = oldsize; |
| 79 while (hi - lo >= 2) { |
| 80 int mid = (lo + hi) / 2; |
| 81 if (std::lexicographical_compare(old + I[mid], old + oldsize, newbuf, |
| 82 newbuf + newsize)) { |
| 83 lo = mid; |
| 84 } else { |
| 85 hi = mid; |
| 86 } |
| 87 } |
| 88 |
| 89 int x = matchlen(old + I[lo], oldsize - I[lo], newbuf, newsize); |
| 90 int y = matchlen(old + I[hi], oldsize - I[hi], newbuf, newsize); |
| 91 if (x > y) { |
| 92 *pos = I[lo]; |
| 93 return x; |
| 94 } |
| 95 *pos = I[hi]; |
| 96 return y; |
| 97 } |
| 98 |
| 99 } // namespace courgette |
| 100 |
| 101 #endif // COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ |
| OLD | NEW |