Skip to content

Commit 1b47452

Browse files
committed
Define memmem using a header file
On Ubuntu 20.04, you need to define `_GNU_SOURCE` before importing `<string.h>` to get `memmem`. It is thus not available when importing `Glibc`.
1 parent 6c4c4cf commit 1b47452

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

Package.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,22 @@ var targets: [Target] = [
111111
dependencies: []
112112
),
113113

114+
.target(
115+
name: "CCompletionScoring",
116+
dependencies: []
117+
),
118+
114119
// MARK: CompletionScoring
115120

116121
.target(
117122
name: "CompletionScoring",
118-
dependencies: [],
123+
dependencies: ["CCompletionScoring"],
119124
swiftSettings: globalSwiftSettings
120125
),
121126

122127
.target(
123128
name: "CompletionScoringForPlugin",
124-
dependencies: [],
129+
dependencies: ["CCompletionScoring"],
125130
swiftSettings: globalSwiftSettings
126131
),
127132

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SOURCEKITLSP_CCOMPLETIONSCORING_H
14+
#define SOURCEKITLSP_CCOMPLETIONSCORING_H
15+
16+
#define _GNU_SOURCE
17+
#include <string.h>
18+
19+
static inline void *sourcekitlsp_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
20+
return memmem(haystack, haystacklen, needle, needlelen);
21+
}
22+
23+
#endif // SOURCEKITLSP_CCOMPLETIONSCORING_H

Sources/CompletionScoring/Utilities/SwiftExtensions.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import CCompletionScoring
1314
import Foundation
1415

15-
#if canImport(Darwin)
16-
import Darwin
17-
#elseif canImport(Glibc)
18-
import Glibc
19-
#elseif canImport(Musl)
20-
import Musl
21-
#elseif canImport(CRT)
22-
import CRT
23-
#elseif canImport(Bionic)
24-
import Bionic
25-
#endif
26-
2716
extension Range where Bound: Numeric {
2817
init(from: Bound, length: Bound) {
2918
self = from..<(from + length)
@@ -447,7 +436,9 @@ extension UnsafeBufferPointer<UInt8> {
447436
guard needle.count > 0, let needleBaseAddress = needle.baseAddress else {
448437
return nil
449438
}
450-
guard let match = memmem(baseAddress + startOffset, count - startOffset, needleBaseAddress, needle.count) else {
439+
guard
440+
let match = sourcekitlsp_memmem(baseAddress + startOffset, count - startOffset, needleBaseAddress, needle.count)
441+
else {
451442
return nil
452443
}
453444
let start = baseAddress.distance(to: match.assumingMemoryBound(to: UInt8.self))

0 commit comments

Comments
 (0)